Terry J. Reedy <tjre...@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 <rep...@bugs.python.org>
<http://bugs.python.org/issue9226>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to