Message 111488 - Python tracker (original) (raw)

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.