I am not a programmer, and now I'm trying to learn python. But I'm a little confused about creating an object. I think the class is like a template, and the object is created (or created) based on the template. Does this mean that after creating the object (for example, classinst1 = MyClass ()), changing the template should not affect what is in the object?
In addition, the code below shows that I can change the variable of the "common" class, but only if I have not assigned 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), change the class variable βcommonβ to no longer affect the value of classinst.common <? >
Can someone explain to me why the code below behaves this way? Is this common to all OO languages, or just one of the bizarre 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
source share