PEP 289: Generator Expressions (please comment)

Holger Krekel pyth at devel.trillke.net
Fri Oct 24 04:13:45 EDT 2003


Raymond Hettinger wrote:
> Peter Norvig's creative thinking triggered renewed interest in PEP 289.
> That led to a number of contributors helping to re-work the pep details
> into a form that has been well received on the python-dev list:
> 
>     http://www.python.org/peps/pep-0289.html
> 
> In brief, the PEP proposes a list comprehension style syntax for
> creating fast, memory efficient generator expressions on the fly:
> 
>     sum(x*x for x in roots)
>     min(d.temperature()*9/5 + 32 for d in days)
>     Set(word.lower() for word in text.split() if len(word) < 5)
>     dict( (k, somefunc(k)) for k in keylist )
>     dotproduct = sum(x*y for x,y in itertools.izip(xvec, yvec))
>     bestplayer, bestscore = max( (p.score, p.name) for p in players )

Although most people on python-dev and here seems to like this PEP I
think it generally decreases readability.  The above constructs seem
heavy and difficult to parse and thus i am afraid that they interfere
with the perceived simplicity of Python's syntax. 

Sometimes it seems that introducing new syntax happens much easier 
than improving or adding stdlib-modules. 

> Each of the above runs without creating a full list in memory,
> which saves allocation time, conserves resources, and exploits
> cache locality.

I do like these properties, though :-)

At least I hope that generator expressions will only be introduced 
via a __future__ statement in Python 2.4 much like it happened with
'yield' and generators.  Maybe the PEP should have an "implementation
plan" chapter mentioning such details? 

cheers,

    holger





More information about the Python-list mailing list