I am not a programmer and I am trying to learn python at the moment. But I am a little confused with the object instantiation. I am thinking Class like a template and object is make(or instantiated) based on the template. Doesn't that mean once object is created(eg. classinst1 = MyClass() ), change in template shouldn't affect what's in the object?
In addition, the below code shows that I could change the class variable "common" but only if I haven't assign a new value to the "common" variable in the object. If I assign a new value to "common" in my object (say classinst1.common = 99), then change my class variable "common" no longer affect classinst.common value????
Can someone please clarify for me why the code below behave such way? Is it common to all OO language or just one of the quirky aspects of python?
===============
>>> class MyClass(object):
... common = 10
... def __init__(self):
... self.myvar=3
... def myfunction(self,arg1,arg2):
... return self.myvar
...
>>> classinst1 = MyClass()
>>> classinst1.myfunction(1,2)
3
>>> classinst2 = MyClass()
>>> classinst2.common
10
>>> classinst1.common
10
>>> MyClass.common = 50
>>> classinst1.common
50
>>> classinst2.common
50
>>> classinst1.common = 99
>>> classinst2.common
50
>>> classinst1.common
99
>>> MyClass.common = 7000
>>> classinst1.common
99
>>> classinst2.common
7000