Python class shared variables gives unexpected output
<p>Based on the explanation on <a href="http://stackoverflow.com/questions/1537202/variables-inside-and-outside-of-a-class-init-function">Shared Variables in Python Class</a> here , I expected the following code to give output as :</p>

<pre><code>123 123
200 200
300 300
</code></pre>

<p>But it is </p>

<pre><code>123 123
200 123
200 300
</code></pre>

<p>Code:</p>

<pre><code>class A:
    abc = 123
    def __init__(self, a,b,c):
        self._a = a
        self._b = b
        self._c = c


if __name__ == '__main__':
    a = A(2, 4, 6)
    b = A(3, 9, 27)

    print a.abc , b.abc
    a.abc = 200
    print a.abc , b.abc
    A.abc = 300
    print a.abc , b.abc
</code></pre>

<p>Can somebody please help understand this ? My impression is that shared variables are same as static variables in C++ classes. Any insights to bust that myth, if it is so, would be helpful too. </p>

<p><a href="http://stackoverflow.com/a/39234412/2373278">@Scott Hunter's answer</a> explains the behavior of code in question best but I would like to add C++ perspective to it here. Unfortunately it couldn't be added as comment in that answer as it is too long. </p>

<p>As in C++, to access static members name needs to be qualified by class name (e.g. <code>A::abc</code> or <code>A::func_static()</code> ) , in Python also you have to access the shared variable outside the class using Class name as qualifier. </p>

<p>Within the class, C++ allows to use the static variable name as like other member variables (i.e. without any qualifier) and same is true with Python as well, as shared variables can be accessed using self such as <code>self.abc</code> here.</p>

<p>Only thing which is different here is Python allows you to create an instance variable with the same name as shared variable for that instance only. </p>

python class oop