Eugene
Eugene

Reputation: 29

Python. Strange class attributes behavior

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

Upvotes: 1

Views: 352

Answers (3)

inv
inv

Reputation: 4869

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

Upvotes: 0

gimel
gimel

Reputation: 86482

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']
>>> 

Upvotes: 4

Amber
Amber

Reputation: 527248

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']

Upvotes: 7

Related Questions