Problem with generator expression and class definition
Michael Spencer
mahs at telcopartners.com
Fri Dec 7 17:25:22 EST 2007
Terry Reedy wrote:
> "Maric Michaud" <maric at aristote.info> wrote in message
> news:20071207164033.0550457C8B at aristote.info...
> |I faced a strange behavior with generator expression, which seems like a
> bug, for both
> | python 2.4 and 2.5 :
>
> Including the latest release (2.5.2)?
>
> | >>> class A :
> | ... a = 1, 2, 3
> | ... b = 1, 2, 3
> | ... C = list((e,f) for e in a for f in b)
> | ...
> | Traceback (most recent call last):
> | File "<stdin>", line 1, in <module>
> | File "<stdin>", line 4, in A
> | File "<stdin>", line 4, in <genexpr>
> | NameError: global name 'b' is not defined
>
> If you switch 'a' and 'b' in the expression, it will be 'a' that is not
> defined.
> There was/is a known issue with generator expressions (but not list comps)
> treating 'for' clauses after the first differently from the first in
> respect to variable resolution. I believe I have seen this discussed on
> PyDev list. But I searched the bugs.python.org for 'generator expression'
> and '_ _s' and got no relevant hits.
>
> | Any comment ? I'm ready to report it as a bug if there is no objection.
>
> If this is in 2.5.2, (and not one else objects), go ahead.
>
> tjr
>
>
>
This behavior is by design (after much much discussion, as Terry says).
See http://www.python.org/dev/peps/pep-0289/#early-binding-versus-late-binding
and the threads it references.
Michael
More information about the Python-list
mailing list