This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author steven.daprano
Recipients abarry, corey, steven.daprano
Date 2016-05-04.15:54:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1462377283.81.0.818135111294.issue26951@psf.upfronthosting.co.za>
In-reply-to
Content
On snap! Nicely found! This seems to have something to do with the way generator expressions are run inside their own scope.

In Python 2.7, a list comprehension in a class sees the locals correctly:

py> class K:
...     a = 2; x = [a+i for i in range(a)]
...
py> K.x
[2, 3]


But change the list comp to a generator expression, and the situation is different:


py> class K:
...     a = 2; x = list(i for i in range(a))  # Okay
...     b = 2; y = list(b+i for i in range(a))  # Raises
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in K
  File "<stdin>", line 3, in <genexpr>
NameError: global name 'b' is not defined


In Python 3, list comps use the same sort of temporary scope as genexprs, and sure enough, the list comp fails with the same error as the generator expression.
History
Date User Action Args
2016-05-04 15:54:43steven.dapranosetrecipients: + steven.daprano, corey, abarry
2016-05-04 15:54:43steven.dapranosetmessageid: <1462377283.81.0.818135111294.issue26951@psf.upfronthosting.co.za>
2016-05-04 15:54:43steven.dapranolinkissue26951 messages
2016-05-04 15:54:43steven.dapranocreate