[Python-Dev] Unbound locals in class scopes

Ron Adam ron3200 at gmail.com
Sat Jun 20 18:39:16 CEST 2015

On 06/20/2015 12:12 PM, Ron Adam wrote:
> On 06/20/2015 02:51 AM, Ivan Levkivskyi wrote:

>> Guido said 13 years ago that this behavior should not be changed:
>> https://mail.python.org/pipermail/python-dev/2002-April/023428.html,
>> however, things changed a bit in Python 3.4 with the introduction of the
>> LOAD_CLASSDEREF opcode. I just wanted to double-check whether it is still a
>> desired/expected behavior.

> Guido's comment still stands as far as references inside methods work in
> regards to the class body. (they must use a self name to access the class
> name space.) But the execution of the class body does use lexical scope,
> otherwise it would print xtop instead of xlocal here.

Minor corrections:

Methods can access but not write to the class scope without using self.  So 
that is also equivalent to the function version using type().  The methods 
capture the closure they were defined in, which is interesting.

And the self name refers to the object's names space not the class name space.

More information about the Python-Dev mailing list