Richard Jones' Log

Mon, 21 Dec 2009
Python's Deficient Generators

I've had people try to explain why Python's generators are deficient before and the light just went off. To flatten an arbitrarily deep (though stack-limited) nested iterator structure, I wrote:

>>> def flatten(iterable):
...  for elem in iterable:
...   if hasattr(elem, '__iter__'):
...    for sub in flatten(elem): yield sub
...   else:
...    yield elem
... 
>>> list(flatten([[1,2,3],[[2,3],4],[4,5,6]]))
[1, 2, 3, 2, 3, 4, 4, 5, 6]

The key failing is that inner "for" loop. It's called "trampolining" as described by David Beazley. If I could somehow yield to the top of the generator stack it'd be unnecessary. It'd look something like PEP 380:

>>> def flatten(iterable):
...  for elem in iterable:
...   if hasattr(elem, '__iter__'):
...    yield from flatten(elem)
...   else:
...    yield elem
... 
>>> list(flatten([[1,2,3],[[2,3],4],[4,5,6]]))
[1, 2, 3, 2, 3, 4, 4, 5, 6]

Too bad there's a moratorium on changes :)

Updated to include the PEP, thanks ulrik!

Tue, 15 Dec 2009
Sharing my "What's New In Python" presentations

I've put together presentations for describing the new features in Python versions 2.4, 2.5 and 2.6. I did this because my employer, like many companies using Python, has only just installed 2.6 - previously almost all development was done using 2.3 (with just a little 2.4, hence I included that version's information as a refresher.)

The slides are on slideshare. Download the Keynote originals though 'cos slideshare burnt the presenter notes onto the slides.

I'll be uploading "What's new in Python 3" some time in late January.

category: Python | permanent link