Saturday, June 04, 2005

Python: Generator Expressions

In one of his talks at PyCon, Alex Martelli said that if you have complicated loop logic, throw it into a generator so that you never have to duplicate it. In the old days, it was common to duplicate loop logic. I like this rule, and I have seen it improve my code. Just in case you haven't seen the syntax yet, it's:

>>> def generate():
... yield 1
... yield 79
... yield 26
... for i in range(0, 10):
... yield i
...
>>> for i in generate():
... print i,
...
1 79 26 0 1 2 3 4 5 6 7 8 9

In Python 2.4, generator expressions were added. When I saw these, I figured, "Well, yeah, that's pretty natural." It took me a while to realize that just as list comprehensions were taken from Haskell, generator expressions are lazy lists from Haskell. Again, here's an example:

>>> import itertools
>>> import random
>>>
>>> numbers = (random.random() for i in itertools.repeat(0))
>>> print "The sum of 10 random numbers is:", sum(itertools.islice(numbers, 10))
The sum of 10 random numbers is: 4.83875818377

No comments: