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>Initially, the class A has an <code>abc</code> defined to be 123, which each of <code>a</code> and <code>b</code> use since neither has an <code>abc</code> of their own.</p>

<p>Then you execute <code>a.abc = 200</code>, which creates an <code>abc</code> for <code>a</code>; <code>b</code> still uses the one from <code>A</code>.</p>

<p>Then you execute <code>A.abc = 300</code>, which updates the <code>abc</code> for <code>A</code>, which <code>b</code> still looks to, so it see the new value.  But <code>a</code> has its own <code>abc</code>, and so doesn't care.</p>

python class oop