I am executing the following Python code :
class Pet :
kind = 'dog' # class variable shared by all instances
tricks = []
def __init__(self, name) :
self.name = name # instance variable unique to each instance
def changePet(self, newPet) :
self.kind = newPet
def addTricks(self, tricks) :
self.tricks.append(tricks)
pet1 = Pet('mypet')
pet2 = Pet('yourpet')
print 'pet1 kind ::: ', pet1.kind;print 'pet2 kind ::: ', pet2.kind
print 'pet1 name ::: ', pet1.name;print 'pet2 name ::: ', pet2.name
Pet.kind = 'cat'
print 'changed Pet.kind to cat'
print 'pet1 kind ::: ', pet1.kind;print 'pet2 kind ::: ', pet2.kind
#changing pet#1 kind does not change pet#2 kind
pet1.changePet('parrot')
print 'changed pet1.kind to parrot'
print 'pet1 kind ::: ', pet1.kind;print 'pet2 kind ::: ', pet2.kind
pet1.addTricks('imitate')
pet2.addTricks('roll over')
print 'pet1 tricks ::: ', pet1.tricks;print 'pet2 tricks ::: ', pet2.tricks
print Pet.__dict__
print pet1.__dict__
print pet2.__dict__
The output is as per the explanation I found on internet. The output is as follows
pet1 kind ::: dog
pet2 kind ::: dog
pet1 name ::: mypet
pet2 name ::: yourpet
changed Pet.kind to cat
pet1 kind ::: cat
pet2 kind ::: cat
changed pet1.kind to parrot
pet1 kind ::: parrot
pet2 kind ::: cat
pet1 tricks ::: ['imitate', 'roll over']
pet2 tricks ::: ['imitate', 'roll over']
{'__module__': '__main__', 'tricks': ['imitate', 'roll over'], 'kind': 'cat', 'addTricks': <function addTricks at 0xb71fa6bc>, 'changePet': <function changePet at 0xb71fa33c>, '__doc__': None, '__init__': <function __init__ at 0xb71fa144>}
{'kind': 'parrot', 'name': 'mypet'}
{'name': 'yourpet'}
Now my query is that why is a mutable class object treated differently than a non-mutable class object