When to refactor your code into generators and how - Jan-Hein Bührman

Tags: python, pun

(One of my summaries of the 2023 Dutch pythonconferentie python meeting in Utrecht, NL).

See https://github.com/jhbuhrman/refactor-into-generators for the presentation with the code examples, as I cannot type in those code examples quickly enough into this summary :-)

You don’t want to repeat yourself. DRY. His example code showed four different kinds of fibionacci functions what just seemed to beg for some refactoring. Splitting out common parts. But turning it into one function with some if/else was possible, but it looked ugly and inelegant.

Especially when you have loops, it often helps to look at your code a bit more conceptually. Often there is a “producing” part and a “consuming” part. Can you split it there, especially with a generator function?

A generator function is a function that uses one or more yield statements. You can use such a generator with .next() or in a loop.

You can go a step further with python’s build-in itertools package. There is also an extra package more-itertools. Some examples:

  • itertools.takewhile(predicate, iterable): continue until the predicate function returns true. Handy to stop when a condition is reached. There’s also .dropwhile().

  • itertools.islice(iterable, start, stop, step): a bit like regular list slicing.

  • more_itertools.one() return the first item and check whether there really is just one item (and not zero or two or more).

  • more_itertools.first() return the first item (or possibly a default).

  • more_itertools.chuncked() helps split large amounts of data in neat manageable chunks.

So: read the itertools documentation and play with it. It is a valuable addition to your python toolbox that can help make your code more elegant and performant.

 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):