PEP 289: Generator Expressions (please comment)
Alex Martelli
aleax at aleax.it
Mon Oct 27 09:11:10 EST 2003
Daniel Dittmar wrote:
> 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?
Nobody ever said it would be. RH's examples were all function
calls because that's an "obvious" use case.
> counter proposal:
> g[(p.score, p.name) for p in players ]
Looks like it's indexing g.
> This works similar to r'\' for raw strings and u'x' for unicode strings.
Nope, it's ambiguous, while <single letter> adjacent to '...' isn't.
> This has the advantage that it can be more easily extended:
> d[(p.name, p.score) for p in players] creates a dictionary
dict((p.name, p.score) for p in players)
will do it in a much more obvious way.
> s[(p.score, p.name) for p in players] creates a sorted list
list.sorted((p.score, p.name) for p in players)
will do it in a much more obvious way, and also allow you to
specify such optional niceties as (e.g.) "key=" (in 2.4 --
and, btw, thanks to all of RH's work on the issue!).
> and
> p[$*-=%/sd/!] allows to embed perl code
If you want perl, you know where to find it.
Alex
More information about the Python-list
mailing list