[Python-Dev] Re: PEP 289 - Generator Expressions - Let's MoveForward

Tim Peters tim.one at comcast.net
Fri Apr 30 17:28:28 EDT 2004

[Russell E. Owen]
> I completely agree. Generator expressions and list comprehensions are
> similar enough that it seems a wart to have to carefully differentiate
> between two in normal use.

In normal use you won't have to distinguish, where "normal use" is pretty
much defined by "cases where it doesn't matter" <wink>.

> Personally, I'd only like to see generator expressions added to Python
> if they can allow list comprehensions to be deprecated -- to avoid
> language clutter.

Listcomps can be deprecated, although not all existing uses of listcomps
will translate directly into genexps.

I think this has become a "practicality beats purity" feature.  When Python
had only 3 scopes, it was fine to ignore "explicit is better than implicit":
the scope model was so bare that explictness would have been overkill.  When
multiple nested scopes got introduced, and function bodies start appearing
in places other than lambda and def (which is the case for genexps), scope
gets much more mysterious (and so also potentially surprising) in the
absence of explicit scope declarations.  But simple genexps will work fine
anyway almost all the time, so it doesn't appear to matter much that devious
uses will have nightmarish semantics.

> One question, though: is "if" part of generator expressions or not?

Yes.  If the PEP isn't clear about this, it's a flaw in the PEP.  I don't
think the semantics of genexps are clear in the PEP either (unless that's
changed recently).

More information about the Python-Dev mailing list