Python. Strange class attributes behavior

Posted by Eugene on Stack Overflow See other posts from Stack Overflow or by Eugene
Published on 2010-04-25T07:13:29Z Indexed on 2010/04/25 7:23 UTC
Read the original article Hit count: 195

Filed under:
|
|
>>> class Abcd:

...     a = ''
...     menu = ['a', 'b', 'c']
... 
>>> a = Abcd()
>>> b = Abcd()
>>> a.a = 'a'
>>> b.a = 'b'
>>> a.a
'a'
>>> b.a
'b'

It's all correct and each object has own 'a', but...

>>> a.menu.pop()
'c'
>>> a.menu
['a', 'b']
>>> b.menu
['a', 'b']

How could this happen? And how to use list as class attribute?

© Stack Overflow or respective owner

Related posts about python

Related posts about class