tags:

views:

52

answers:

3

For class A, why is aMap member variable being shared between object and object b?

>>> class A:
...     aMap = {}

>>> a = A()
>>> a.aMap["hello"] = 1

>>> b = A()
>>> b.aMap["world"] = 2

>>> c = []
>>> c.append(a)
>>> c.append(b)

>>> for i in c:
...     for j in i.aMap.items():
...         print j
('world', 2)  
('hello', 1)  
('world', 2)  
('hello', 1)  
+4  A: 

Because you defined it as a class attribute, not instance attribute.

If you wish to have it as instance attribute and not be shared between instances, you have to define it like this:

class A(object):
    def __init__(self):
        self.aMap = {}
gruszczy
+1: Class attributes are shared among objects. Be careful of vague phrases like "member variable". Try to talk about "instance attributes" and "class attributes", not "member variables", which is vague.
S.Lott
Always inherit `object`.
Mike Graham
True, especially since it's the only way to use properties, when you need them in the future :-)
gruszczy
+3  A: 

Because its a class attribute, not an instance attribute ("member variable").

To make it a instance attribute, assign it on an instance, e.g. in the constructor:

class A:
    def __init__(self):
        self.aMap = {}

But you could also do:

a = A()
a.aMap = {}
oefe
Always inherit `object`.
Mike Graham
A: 

Because you are effectively defining a class variable instead of an instance variable.

To define an instance variable, you should do it on a method referring the object itself (generally self):

class A(object):
    class_variable = 0

    def __init__(self, a, b):
        self.instance_variable = a
        class_variable = b

>>> a=A(1,2)
>>> print a.instance_variable
1
>>> print a.class_variable
2
>>> b=A(2,1)
>>> print b.instance_variable
2
>>> print b.class_variable
1
>>> print a.instance_variable
1
>>> print a.class_variable
1
voyager