PEP 289: Generator Expressions (please comment)

Alex Martelli aleax at aleax.it
Sat Oct 25 17:21:57 EDT 2003


Ian McMeans wrote:

> I think that using the yield keyword makes it more obviously a
> generator.

...but makes it harder to rapidly eyeball if a function which
USES yield is or isn't a generator, which is much more relevant
because it changes all semantics.

I.e., if right now I see a def without further embedded defs and
at a glance I see...:

def f ...
   ...
   ... yield ...
   ...

I already know f is a generator.  If the keyword 'yield' was
overloaded for other purposes, I would have to stop and check
out the details very very carefully.


> sum(x for x in lst) makes it clear what it will compute, but not that
> it will be computed using a generator. sum(yield x for x in lst) makes
> it more apparent that you're creating a generator and passing it to
> sum.

Yeah, but the fact that what I'm passing is (the result from calling)
a generator is a relatively irrelevant implementation detail (which is
why I preferred to call these "iterator expressions", but was overruled,
oh well).  All that matters is that I'm passing an _iterator_ that will
behave in a specific way, without O(N) memory consumption, not how that
iterator is internally implemented.

I'd much rather omit the 'yield' for two reasons, therefore: [a] keep
it easy to see if a function is actually a generator (which does matter
A LOT!), [b] avoid inappropriate focus on implementation detaiils vs
semantical and performance indications.


Alex





More information about the Python-list mailing list