Recently I found the need to generate some constants inside a class body. What I discovered was a bit unintuitive, and may not be intended... In 2.5 and 2.6: >>> class foo: ... x = {} ... x.update((i, x.get(i, None)) for i in xrange(10)) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in foo File "<stdin>", line 3, in <genexpr> NameError: global name 'x' is not defined >>> class foo: ... x = {} ... x.update([(i, x.get(i, None)) for i in xrange(10)]) ... >>> In 3.0: >>> class foo(): ... x = {} ... x.update((i, x.get(i, None)) for i in range(10)) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in foo File "<stdin>", line 3, in <genexpr> NameError: global name 'x' is not defined >>> class foo(): ... x = {} ... x.update([(i, x.get(i, None)) for i in range(10)]) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in foo File "<stdin>", line 3, in <listcomp> NameError: global name 'x' is not defined >>> The behavior of 3.0 WRT list comprehensions behaving the same way as generator expressions is expected, but why generator expressions (generally) don't keep a reference to the class scope during execution seems to be unintended. - Josiah
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4