November 03, 2003
Generator Expressions

PEP 289 -- Generator Expressions

I like this proposal. (Sorry, Hans!)

Python's List Comprehensions have long been a favourite of mine - powerful, and terse without being difficult to read or, uh, comprehend. (Those with only one for, that is. List comprehensions with multiple fors overflow my brain.)

But I must admit - I don't like the name much. 'Comprehension' - what does that have to do with anything?

List comprehensions always create the whole list. You don't always need the whole list at once, and if the list is big (or contains big objects), this might be a problem memory-wise. And if course, if your 'list' is infinite you can't materialise it at all! Python's generators are perfect for dealing with large or potentially infinite sequences.

PEP 289 proposes a list comprehension like syntax for creating generators - the generator expression.

Since the output of any finite generator can be turned into a list using the list() built-in, the existing list comprehension syntax will become merely syntax sugar for a generator expression so wrapped. But by wrapping in calls to dict() or tuple() or so on, one can create dictionaries and tuples as easily as lists.

Also, generator comprehensions are a natural fit with reduction functions like sum() - why materialise a whole list at once just to add up the numbers? New reduction functions are also planned to take advantage - though I hope these go into a module, rather than bulk out the built-ins too much.

All this, and a name that makes sense!

Posted to Python by Simon Brunning at November 03, 2003 05:11 PM
Comments

:-) Maybe I'm just becoming a grumpy young man or something...

Posted by: Hans on November 4, 2003 12:12 AM

The name for list comprehensions comes from Haskell where the idea and design was lifted.

And as for list comprehensions being just syntax sugar for a genexp past to list(), that is not completely true thanks to late binding. This was part of the argument against late binding in the first place. While it is a little hard to come up with a situation where this isn't true, it does come up if you don't consume the generator expression fairly quickly.

Posted by: Brett on June 23, 2004 05:44 PM

I was aware of where the name came from, but I still don't know what it *means*. ;-)

Hmmm. Surely if you pass the genexp into list, the whole generator is consumed immediately. Thus the late/early binding distinction isn't relevant, so a list comprehension *is* exactly the same as list(genexp).

Posted by: Simon Brunning on June 24, 2004 09:06 AM
Post a comment
Name:


Email Address:


URL:



Comments:


Remember info?