views:

83

answers:

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

+6  A: 

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']
Amber
But, that about different between simple string attribute and list attribute?Why?
Eugene
Because it's the difference between a reference and a value. If you did `a.menu = ['a','b']`, then `b.menu` would be left unchanged - but you're not, you're changing the list *pointed to* by `a.menu`.
Amber
+4  A: 

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']
>>> 
gimel
You might want to note that all classes are automatically new-style classes in the Python 3.0+ line.
Michael Aaron Safyan
A: 

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

forgot