Python. Strange class attributes behavior

2019-07-30 15:38发布

>>> 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?

3条回答
不美不萌又怎样
2楼-- · 2019-07-30 15:57

because variables in Python are just "labels"

both Abcd.menu and a.menu reference the same list object.

in your case you should assign the label to a new object,

not modify the object inplace.

You can run

a.menu = a.menu[:-1]

instead of

a.menu.pop()

to feel the difference

查看更多
Bombasti
3楼-- · 2019-07-30 16:06

See class-objects in the tutorial, and notice the use of self.

Use instance attributes, not class attributes (and also, new style classes) :

>>> class Abcd(object):
...     def __init__(self):
...         self.a = ''
...         self.menu = ['a','b','c']
...         
>>> a=Abcd()
>>> b=Abcd()
>>> a.a='a'
>>> b.a='b'
>>> a.a
'a'
>>> b.a
'b'
>>> a.menu.pop()
'c'
>>> a.menu
['a', 'b']
>>> b.menu
['a', 'b', 'c']
>>> 
查看更多
叛逆
4楼-- · 2019-07-30 16:13

This is because the way you're initializing the menu property is setting all of the instances to point to the same list, as opposed to different lists with the same value.

Instead, use the __init__ member function of the class to initialize values, thus creating a new list and assigning that list to the property for that particular instance of the class:

class Abcd:
    def __init__(self):
        self.a = ''
        self.menu = ['a', 'b', 'c']
查看更多
登录 后发表回答