Class Variable Access and Assignment

Christopher Subich csubich.spam.block at spam.subich.block.com
Fri Nov 4 10:31:42 EST 2005


Antoon Pardon wrote:
> Well I wonder. Would the following code be considered a name binding
> operation:
> 
>   b.a = 5

Try it, it's not.

Python 2.2.3 (#1, Nov 12 2004, 13:02:04)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-42)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> a
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
NameError: name 'a' is not defined
 >>> b = object()
 >>> b.a
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: 'object' object has no attribute 'a'

Once it's attached to an object, it's an attribute, not a base name. 
The distinction is subtle and possibly something that could (should?) be 
unified for Py3k, but in cases like this the distinction is important.



More information about the Python-list mailing list