Note that there are some explanatory texts on larger screens.

plurals
  1. POplease help me understanding python object instantiation.
    primarykey
    data
    text
    <p>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. <strong>Doesn't that mean once object is created(eg. classinst1 = MyClass() ), change in template shouldn't affect what's in the object?</strong></p> <p>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. <strong>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????</strong></p> <p>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? </p> <p>===============</p> <pre><code>&gt;&gt;&gt; class MyClass(object): ... common = 10 ... def __init__(self): ... self.myvar=3 ... def myfunction(self,arg1,arg2): ... return self.myvar ... &gt;&gt;&gt; classinst1 = MyClass() &gt;&gt;&gt; classinst1.myfunction(1,2) 3 &gt;&gt;&gt; classinst2 = MyClass() &gt;&gt;&gt; classinst2.common 10 &gt;&gt;&gt; classinst1.common 10 &gt;&gt;&gt; MyClass.common = 50 &gt;&gt;&gt; classinst1.common 50 &gt;&gt;&gt; classinst2.common 50 &gt;&gt;&gt; classinst1.common = 99 &gt;&gt;&gt; classinst2.common 50 &gt;&gt;&gt; classinst1.common 99 &gt;&gt;&gt; MyClass.common = 7000 &gt;&gt;&gt; classinst1.common 99 &gt;&gt;&gt; classinst2.common 7000 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload