[issue9226] erroneous behavior when creating classes inside a closure

Terry J. Reedy report at bugs.python.org
Sun Jul 25 00:21:33 CEST 2010


Terry J. Reedy <tjreedy at udel.edu> added the comment:

Guido clarified:
> Class scopes *do* behave differently from function scopes;
> outside a nested function, this should work:
> x = 1
> class C(object):
>   x = x
> assert C.x == 1
> And I think it should work that way inside a function too.

I take that to mean that

x = 0
def f()
  x = 1
  class C(object):
    x = x
  assert C.x == 1
f()

should work, meaning that C.x==0 and UnboundLocalError are both wrong.

That would mean to me that in "The class’s suite is then executed in a new execution frame (see section Naming and binding), using a newly created local namespace and the original global namespace." the phrase "the original global namespace" should be changed to "the surrounding namespaces".

I also think this from Guido

"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"."

should somehow also be clearer, probably also in the class page, so that people will neither expect an UnboundLocalError.

----------

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


More information about the Python-bugs-list mailing list