[Python-3000] Change to class construction? (original) (raw)

Georg Brandl g.brandl at gmx.net
Fri Jul 6 17:00:08 CEST 2007


Collin Winter schrieb:

While experimenting with porting setuptools to py3k (as of r56155), I ran into this situation:

class C: a = (4, 5) b = [c for c in range(2) if a] results in a "NameError: global name 'a' is not defined" error, while class C: a = (4, 5) b = [c for c in a] works fine. This gives the same error as above: class C: a = (4, 5) b = [a for c in range(2)] Both now-erroneous snippets work in 2.5.1. Was this change intentional?

It is at least intentional in the sense that in 3k it works the same as with genexps, which give the same errors in 2.5.

What's different is that all code inside a genexp except the first iterator (which is why the second example works) is contained in its own function namespace.

So, an equivalent problem is:

class C: foo = 1 def bar(): print(foo) bar()

Georg

-- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.



More information about the Python-3000 mailing list