Understanding behaviour of self with static variables
<p>I am confused with the behaviour of self when it comes to dealing with static variables in python.From what I understand is that static variables can be accessed by either using <code>classname.variablename</code> or <code>self.variablename</code>. However changing the value of that variable differs. I realized that if i change the static variable value by <code>classname.variablename=SomeValue</code> the instance variable reflects that value however if I change the value of static variable using <code>self.variablename=SomeValue</code> the static variable does not change when access like <code>classname.variablename</code> from what I understand is that when I assign a value like <code>self.variablename=SomeValue</code> then an instance variable is created. Can somebody please shed a little light on this behaviour.</p>

<p>Example 1:</p>

<pre><code>class bean:
    mycar="SomeCar"
    def test(self):
        bean.mycar = "yup"
        print(self.mycar) #prints yup
</code></pre>

<p>Example 2:</p>

<pre><code>class bean:
        mycar="SomeCar"
        def test(self):
            self.mycar = "pup"
            print(bean.mycar) #SomeCar
</code></pre>

<p><code>self</code> in python is an ordinary name that binds the reference to the <em>instance</em> calling a class method. It is passed as the first argument to a method and by convention, it is bound to the name 'self'. When <code>self.variable = value</code> is called, you are setting the value of an instance variable; a variable unique to that <em>particular</em> <code>bean</code>. </p>

<p>For example, <code>self.name = "Fred"</code> might name my mother's bean, but I named my own bean "George" when I called <code>self.name</code> from <em>my</em> bean. </p>

<p>On the other hand, <code>bean.name = "Yousef"</code> names <em>all</em> beans. My mother's bean is now named "Yousef", and so is mine. </p>

<p>If my Dad has a bean as well, he'll be surprised to find out that it too, is named "Yousef" when he calls <code>bean.name</code>. But he can still use <code>self.name</code> to give his bean its own (possibly unique) name.</p>

<h2>Example Code:</h2>

<pre><code>class bean:
    name = "Yousef"  # All beans have this name with `bean.name`

moms = bean()
mine = bean()
dads = bean()

beans = [moms, mine, dads]

# Primitive tabular output function
def output(bean_list):
    print("-bean-", "\t", "-self-")
    for b in bean_list:
        print(bean.name, "\t", b.name)
    print("") # Separate output sets with a newline

# Print the names with only the class attribute set
output(beans)

# Python magic using zip to apply names simultaneously
# Mom's bean is "Fred", mine is "George"
# My dad is weird and named his "Ziggaloo"
for b, n in zip(beans, ["Fred", "George", "Ziggaloo"]):
    b.name = n

# Print the names after applying `self.name`
output(beans)
</code></pre>

<h2>Python 3.4 Output:</h2>

<pre><code>-bean-   -self-
Yousef   Yousef
Yousef   Yousef
Yousef   Yousef

-bean-   -self-
Yousef   Fred
Yousef   George
Yousef   Ziggaloo
</code></pre>

python static-variables