Message264830
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. |
|
Date |
User |
Action |
Args |
2016-05-04 15:54:43 | steven.daprano | set | recipients:
+ steven.daprano, corey, abarry |
2016-05-04 15:54:43 | steven.daprano | set | messageid: <1462377283.81.0.818135111294.issue26951@psf.upfronthosting.co.za> |
2016-05-04 15:54:43 | steven.daprano | link | issue26951 messages |
2016-05-04 15:54:43 | steven.daprano | create | |
|