Let's explore a bit. This is your class:
class Class:
var1 = 'in class'
def __init__(self, var1):
self.var1 = var1
c = Class('in inst')
Accessing c.var1
shows the var1
from the instance:
>>> c.var1
'in inst'
It is located inside __dict__
of c
:
>>> c.__dict__
{'var1': 'in inst'}
The class variable var1
is in __dict__
of Class
:
>>> Class.__dict__['var1']
'in class'
Now make a class with only a class variable:
class Class2:
var1 = 'in class'
c2 = Class2()
Since there is nothing in the instance, var1
from the class will be used:
>>> c2.var1
'in class'
The __dict__
of c2
is empty:
>>> c2.__dict__
{}
Python always looks for var1
first in the instance and than falls back to the class.