[issue9226] erroneous behavior when creating classes inside a closure

Guido van Rossum report at bugs.python.org
Sun Jul 25 01:10:12 CEST 2010


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

On Sat, Jul 24, 2010 at 3:21 PM, Terry J. Reedy <report at bugs.python.org> wrote:
>
> 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.

Indeed.

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

Those words sound like they were never revised since I wrote them for
Python 0.9.8 or so...

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

FWIW, unless something drastically changed recently, the language
reference is likely out of date in many areas. I would love it if a
team of anal retentive freaks started going through it with a fine
comb so as to make it describe the state of the implementation(s) more
completely.

----------

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


More information about the Python-bugs-list mailing list