Generator expressions v/s list comprehensions

Alex Martelli aleaxit at yahoo.com
Mon Aug 30 04:07:40 EDT 2004


Mahesh Padmanabhan <mahesh at privacy.net> wrote:

> In article <87llfywooz.fsf at sinken.local.csis.hku.hk>,
>  Isaac To <iketo2 at netscape.net> wrote:
> 
> [snip]
> 
> Thanks Isaac for providing such a detailed explanation. I understand now
> why generator expressions make more sense. 
> 
> I just wish that generator expressions had been designed into the 
> language without going the way of list comprehension -> generator 
> expressions.

It would sure be nice if Python had been born back from day one with all
the neat features it has taken years to develop -- that way we wouldn't
have any issues that are there just because of backwards compatibility.

Unfortunately, this wish is totally unrealistic -- obviously people do
come up with cool ideas such as the iterator protocol, and generators,
after the language has been around for a while.  When list
comprehensions got into Python the new iterator protocol wasn't there
yet, so of course generator comprehensions could not have been thought
of.  Similarly, dicts now have both (e.g.) a method .items() which
returns a list, and a method .iteritems() which returns an iterator --
no special reason for both to be there, but the former was there first,
before the iterator protocol, so it must stay for backwards
compatibility.  One day (Python 3.0) we'll be able to break
compatibility and simplify things, but that sort of thing can be allowed
only very rarely in a language's continuing evolution.


Alex



More information about the Python-list mailing list