[Python-au] Class attribute behaviour wierdisms

Darryl Ross spam at afoyi.com
Tue Feb 21 05:15:29 CET 2006


Hey All,

Just wondering whether this would be expected behaviour??

> darryl at mail:~$ cat test.py
> class Blah:
>     attr = []
> 
> for i in range(10):
>     obj = Blah()
>     obj.attr.append('123')
>     print obj.attr


> darryl at mail:~$ python test.py
> ['123']
> ['123', '123']
> ['123', '123', '123']
> ['123', '123', '123', '123']
> ['123', '123', '123', '123', '123']
> ['123', '123', '123', '123', '123', '123']
> ['123', '123', '123', '123', '123', '123', '123']
> ['123', '123', '123', '123', '123', '123', '123', '123']
> ['123', '123', '123', '123', '123', '123', '123', '123', '123']
> ['123', '123', '123', '123', '123', '123', '123', '123', '123', '123']


I would have thought that by instantiating a new object, it should be a
copy of the definition, not a reference to the previous object?

Any ideas?

Regards
Darryl


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 208 bytes
Desc: OpenPGP digital signature
Url : http://starship.python.net/pipermail/python-au/attachments/20060221/417efed2/signature.pgp


More information about the python-au mailing list