[issue9226] erroneous behavior when creating classes inside a closure

Guido van Rossum report at bugs.python.org
Sat Jul 24 17:23:22 CEST 2010


Guido van Rossum <guido at python.org> added the comment:

Hm. This seems an old bug, probably introduced when closures where first introduced (2.1 ISTR, by Jeremy Hylton).

Class scopes *do* behave differently from function scopes; outside a nested function, this should work:

x = 1
class C(object):
  x = x
assert X.x == 1

And I think it should work that way inside a function too.

So IMO the bug is that in classes Test and Test3, the x defined in the function scope is not used.  Test2 shows that normally, the x defined in the inner scope is accessed.

So, while for *function scopes* the rules are "if it is assigned anywhere in the function, every reference to it references the local version", for *class scopes* (outsided methods) the lookup rules are meant to be dynamic, meaning "if it isn't defined locally yet at the point of reference, use the next outer definition".

I haven't reviewed the patches.

----------

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


More information about the Python-bugs-list mailing list