Class Variable Access and Assignment

Antoon Pardon apardon at forel.vub.ac.be
Thu Nov 3 08:44:02 EST 2005


Op 2005-11-03, Sybren Stuvel schreef <sybrenUSE at YOURthirdtower.com.imagination>:
> Antoon Pardon enlightened us with:
>> I would expect a result consistent with the fact that both times b.a
>> would refer to the same object.
>
> "b.a" is just a name, not a pointer to a spot in memory. Getting the
> value associated with that name is something different from assigning
> a new value to that name.

If that was all to it, one would expect the following to work to:

>>> a = 1
>>> def f():
...   a += 2
...
>>> f()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
UnboundLocalError: local variable 'a' referenced before assignment

But I'll word it differently:

I would expect a result consistent with the fact that both times
b.a would be resolved in the same name space.




More information about the Python-list mailing list