[issue9226] erroneous behavior when creating classes inside a closure

Benjamin Peterson report at bugs.python.org
Sun Jul 11 22:24:45 CEST 2010


Benjamin Peterson <benjamin at python.org> added the comment:

I'm not sure what I correct behavior is in this case. Consider the function equivalent:

x = 3
def f(x):
    def m():
        x = x
        print x
    m()
f(4)

which gives:

Traceback (most recent call last):
  File "x.py", line 7, in <module>
    f(4)
  File "x.py", line 6, in f
    m()
  File "x.py", line 4, in m
    x = x
UnboundLocalError: local variable 'x' referenced before assignment

The class example works because name namespaces are unoptimized, so failing to find a binding in the local (class) namepsace, Python looks at the globals and finds the global definition.

----------
nosy: +benjamin.peterson

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9226>
_______________________________________


More information about the Python-bugs-list mailing list