Class Variable Access and Assignment

Christopher Subich csubich.spam.block at spam.subich.block.com
Fri Nov 4 12:12:00 EST 2005


Antoon Pardon wrote:

> Except when your default is a list
> 
> class foo:
>   x = [] # default
> 
> a = foo()
> a.x += [3]
> 
> b = foo()
> b.x
> 
> This results in [3]. So in this case using a class variable x to
> provide a default empty list doesn't work out in combination
> with augmented operators.

This has nothing to do with namespacing at all, it's the Python 
idiosyncracy about operations on mutable types.  In this case, += 
mutates an object, while + returns a new one -- as by definition, for 
mutables.



More information about the Python-list mailing list