PEP 289: Generator Expressions (please comment)

Daniel Dittmar daniel at dittmar.net
Thu Oct 23 16:50:21 EDT 2003


Raymond Hettinger wrote:
>     bestplayer, bestscore = max( (p.score, p.name) for p in players )
> 
> Each of the above runs without creating a full list in memory,
> which saves allocation time, conserves resources, and exploits
> cache locality.

Why should generator comprehension be useful only for arguments?

counter proposal:
g[(p.score, p.name) for p in players ]

This works similar to r'\' for raw strings and u'x' for unicode strings.

This has the advantage that it can be more easily extended:
d[(p.name, p.score) for p in players] creates a dictionary
s[(p.score, p.name) for p in players] creates a sorted list
and
p[$*-=%/sd/!] allows to embed perl code

Daniel





More information about the Python-list mailing list