Class Variable Access and Assignment

Antoon Pardon apardon at forel.vub.ac.be
Thu Nov 3 09:46:50 EST 2005


Op 2005-11-03, venk schreef <venkatasubramanian at gmail.com>:
> Again (blink) quoting from the docs "
> For targets which are attribute references, the initial value is
> retrieved with a getattr() and the result is assigned with a setattr().
> Notice that the two methods do not necessarily refer to the same
> variable. When getattr() refers to a class variable, setattr() still
> writes to an instance variable. For example:
>
> class A:
>     x = 3    # class variable
> a = A()
> a.x += 1     # writes a.x as 4 leaving A.x as 3
> "
>
> I felt a wee bit clear after going thru the doc... attribute
> referencing is not the same as searching the variable in the enclosing
> scopes.... But, i still feel the inconsistency...

The documentation is IMO no help in arguing whether the behaviour is
sane or not. Unsane documented behaviour is still unsane.

So yes the documentation explains why such a result can happen,
but that doesn't change the fact that in this case we have
only one a.x on that line and the processing behind the scenes
refers to an x in two different name spaces.

I think that is unsane behaviour and giving an explanation on
why this particular behaviour arises from the current implemenation
doesn't change that.

-- 
Antoon Pardon



More information about the Python-list mailing list