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
statements. You can use such a generator with
.next() or in a loop.
itertools.takewhile(predicate, iterable): continue until the
predicate function returns true. Handy to stop when a condition is
reached. There’s also
itertools.islice(iterable, start, stop, step): a bit like regular list
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
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.
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.
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):