I'll put the code first:
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Item(object):
... def __init__(self, name, value={}):
... self.name = name
... self.value = value
... def __str__(self):
... return self.name + " - " + str(self.value)
... def addValues(self, value):
... for key,price in value.iteritems():
... self.value[key] = price
...
>>>
>>> item1 = Item('car')
>>> print item1
car - {}
>>> item2 = Item('truck')
>>> print item2
truck - {}
>>> item1.addValues({'blue':6000})
>>> print item1
car - {'blue': 6000}
>>> print item2
truck - {'blue': 6000}
>>>
I created two instances of class Item, item1 and item2. Then, I changed the value of dictionary attribute on object item1 with addValues method. The problem is that, adding dictionary attribute of item1, also added the same value to the item2. Can someone explain what is happening here? How did changing value on item1 changed the value on item2? Did I overlooked something?