This is because of the way Python resolves names with the .. When you write self.list the Python runtime tries to resolve the list name first by looking for it in the instance object, and if it is not found there, then in the class instance.

Let's look into it step by step

self.list.append(1)
  1. Is there a list name into the object self?
    • Yes: Use it! Finish.
    • No: Go to 2.
  2. Is there a list name into the class instance of object self?
    • Yes: Use it! Finish
    • No: Error!

But when you bind a name things are different:

self.list = []
  1. Is there a list name into the object self?
    • Yes: Overwrite it!
    • No: Bind it!

So, that is always an instance variable.

Your first example creates a list into the class instance, as this is the active scope at the time (no self anywhere). But your second example creates a list explicitly in the scope of self.

More interesting would be the example:

class testClass():
    list = ['foo']
    def __init__(self):
        self.list = []
        self.list.append('thing')

x = testClass()
print x.list
print testClass.list
del x.list
print x.list

That will print:

['thing']
['foo']
['foo']

The moment you delete the instance name the class name is visible through the self reference.

Answer from rodrigo on Stack Overflow
Top answer
1 of 4
67

This is because of the way Python resolves names with the .. When you write self.list the Python runtime tries to resolve the list name first by looking for it in the instance object, and if it is not found there, then in the class instance.

Let's look into it step by step

self.list.append(1)
  1. Is there a list name into the object self?
    • Yes: Use it! Finish.
    • No: Go to 2.
  2. Is there a list name into the class instance of object self?
    • Yes: Use it! Finish
    • No: Error!

But when you bind a name things are different:

self.list = []
  1. Is there a list name into the object self?
    • Yes: Overwrite it!
    • No: Bind it!

So, that is always an instance variable.

Your first example creates a list into the class instance, as this is the active scope at the time (no self anywhere). But your second example creates a list explicitly in the scope of self.

More interesting would be the example:

class testClass():
    list = ['foo']
    def __init__(self):
        self.list = []
        self.list.append('thing')

x = testClass()
print x.list
print testClass.list
del x.list
print x.list

That will print:

['thing']
['foo']
['foo']

The moment you delete the instance name the class name is visible through the self reference.

2 of 4
10

Python has interesting rules about looking up names. If you really want to bend your mind, try this code:

class testClass():
    l = []
    def __init__(self):
        self.l = ['fred']

This will give each instance a variable called l that masks the class variable l. You will still be able to get at the class variable if you do self.__class__.l.

The way I think of it is this... Whenever you do instance.variable (even for method names, they're just variables who's values happen to be functions) it looks it up in the instance's dictionary. And if it can't find it there, it tries to look it up in the instance's class' dictionary. This is only if the variable is being 'read'. If it's being assigned to, it always creates a new entry in the instance dictionary.

🌐
GeeksforGeeks
geeksforgeeks.org › python › g-fact-34-class-or-static-variables-in-python
Class (Static) and Instance Variables in Python - GeeksforGeeks
Class variables are shared by all objects of a class, whereas instance variables are unique to each object. Unlike languages such as Java or C++, Python does not require a static keyword.
Published: March 6, 2026
Discussions

Can someone explain how object/instance variables vs class/static variables work in Python?
In the first example, you get an error because the attribute, var1, hasn't been defined before you use it in an expression. So, you would need to do this. class A: def __init__(self): pass def some_func(self): self.var1 += 1 a = A() a.var1 = 0 # create attribute a.some_func() print(a.var1) In your second example, as you've defined a class variable, that is used to define the instance variable the first time you use a local assignment expression in the instance method. Thus, in the below, you will find that a has an instance variable but b doesn't, class A: var1 = 0 def __init__(self): pass def some_func(self): self.var1 += 1 a = A() b = A() a.some_func() print(a.var1, b.var1) print(vars(a), vars(b)) # outputs all the attributes of the instances PS. If I modify the class variable, A.var1 += 10 then you would find b.var1 was also changed (because it is pointing to the same object). More on reddit.com
🌐 r/learnpython
17
39
July 6, 2023
Are all class variables in Python always defined for the first time within the __init__?
You typically assign instance variables within __init__ (that is, variables that belong to each instance of a class). You can define class variables (that is, variables that the class object itself should have, and each instance can share) outside of __init__ though. class Foo: bar = "bar" def __init__(self): self.foo = "foo" print(Foo.bar) # bar print(Foo.foo) # AttributeError: type object 'Foo' has no attribute 'foo' x = Foo() print(x.bar) # bar print(x.foo) # foo Foo.bar = "new_value" print(x.bar) # new_value More on reddit.com
🌐 r/learnpython
10
4
October 30, 2019
When to use class variables vs instance variables
Codecademy is using a regular local variable here, not a class attribute. More on reddit.com
🌐 r/learnpython
25
2
February 9, 2023
Can someone explain "_" and "__" in python clearly for me ?
There is no inherent purpose, it's just convention. Python doesn't have real enforced scoping, it just assumes you know not to mess with things you're explicitly asked to not mess with. More on reddit.com
🌐 r/learnpython
49
109
January 17, 2022
🌐
Python documentation
docs.python.org › 3 › tutorial › classes.html
9. Classes — Python 3.14.8 documentation
1 week ago - When the method object is called ... are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class:...
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-class-and-instance-variables-in-python-3
Understanding Class and Instance Variables in Python 3 | DigitalOcean
Learn the difference between class and instance variables in Python 3 with clear examples, a comparison table, and FAQs. Write better OOP code today.
🌐
PYnative
pynative.com › home › python › python object-oriented programming (oop) › python class variables
Python Class Variables With Examples – PYnative
September 8, 2023 - All instances of a class share class variables. Unlike instance variable, the value of a class variable is not varied from object to object, In Python, class variables are declared when a class is being constructed. They are not defined inside any method of a class.
🌐
Mooc
programming-25.mooc.fi › part-9 › 5-class-attributes
Class attributes - Python Programming MOOC 2025
The variable name cls is similar to the self parameter. The difference is that cls points to the class while self point to an instance of the class. Neither parameter is included in the argument list when the function is called; Python fills in the appropriate value automatically.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › can someone explain how object/instance variables vs class/static variables work in python?
r/learnpython on Reddit: Can someone explain how object/instance variables vs class/static variables work in Python?
July 6, 2023 -

So I come from a Java background where defining, declaring and accessing static and instance level variables are pretty much a straightforward process. I want to be able to understand OOP concepts of Python properly so I have been doing some practice.

I have a class:

class A:

def init(self): pass

def someFunc(self): self.var1 += 1

I create an object of this class and call the someFunc() method:

a = A() 
a.someFunc()

It gives me an error. Ok, fair enough since I haven't declared a self.var1 variable yet.

Consider another example.

class A:

var1 = 10

def init(self): pass

def someFunc(self): self.var1 += 1

Now when I do this:

a = A()
a.someFunc()

Output: 11

I know that variables defined just below the class definition are class/static variables. And to access them you have to do A.var1

But why does it not give me an error now? I haven't created a object/instance level self.var1 variable yet, just a class level variable var1.

And when I call A.var1 the output is 10. Why is the output not the same as a.var1?

Does python automatically use the class level variable with the same name since there is no instance level variable defined with the same name? And does that in turn become a different variable from the class level variable?

Can someone please elaborate?

Top answer
1 of 11
9
In the first example, you get an error because the attribute, var1, hasn't been defined before you use it in an expression. So, you would need to do this. class A: def __init__(self): pass def some_func(self): self.var1 += 1 a = A() a.var1 = 0 # create attribute a.some_func() print(a.var1) In your second example, as you've defined a class variable, that is used to define the instance variable the first time you use a local assignment expression in the instance method. Thus, in the below, you will find that a has an instance variable but b doesn't, class A: var1 = 0 def __init__(self): pass def some_func(self): self.var1 += 1 a = A() b = A() a.some_func() print(a.var1, b.var1) print(vars(a), vars(b)) # outputs all the attributes of the instances PS. If I modify the class variable, A.var1 += 10 then you would find b.var1 was also changed (because it is pointing to the same object).
2 of 11
5
In python classes a reference to self.varname first looks for an instance attribute varname. If found, it is used. But if not found the class variable varname is used, if it exists. When assigning to self.varname an instance attribute is always created or updated, leaving any class variable of the same name unchanged. You can see that in this code, based on your second example: class A: var1 = 10 # no need to use "pass" in __init__() def someFunc(self): self.var1 += 1 a = A() a.someFunc() print(f"{a.var1=}, {A.var1=}") Run that code and you will see the instance variable is 11 and the class variable remains unchanged at 10. It's good practice to always refer to class variables by A.var1 so you don't fall into the trap of thinking assigning to self.var1 changes the class variable of name var1.
🌐
Manifoldapp
cuny.manifoldapp.org › read › how-to-code-in-python-3 › section › 2d19fb29-ab07-41aa-9981-167b097115c5
Understanding Class and Instance Variables | How To Code in Python 3 | Manifold @CUNY
Variables are essentially symbols that stand in for a value you’re using in a program. At the class level, variables are referred to as class variables, whereas variables at the instance level are called instance variables.
🌐
CBT Nuggets
cbtnuggets.com › blog › technology › programming › python-class-variables-explained
Python Class Variables: Explained
Here is a good way to sum up the relationship: a class variables is shared by all objects that are created. An instance of the class variable is always created on each newly minted object; it overrides the class instance. Lastly, an instance variable is only accessible to the object it was defined in. Warning: The following code will only work in Python 3.x
🌐
IONOS
ionos.com › digital guide › websites › web development › python class variables
How to create and use Python class variables
July 15, 2024 - Python class variables are useful for storing data that applies to all instances of a class, such as con­fig­u­ra­tion settings, default values or shared states across a class hierarchy.
🌐
Reddit
reddit.com › r/learnpython › are all class variables in python always defined for the first time within the __init__?
r/learnpython on Reddit: Are all class variables in Python always defined for the first time within the __init__?
October 30, 2019 -

In Java, you declare all the variables of your class within the class but outside of any methods. You can then assign the variables accordingly within the methods of the class.

But in Python, I don't see any variables outside of methods. Most of the variables are used for the first time in the init method. That just seems a bit strange to me, but is this just how it works? You define in the init any variables you want an object of the class to have?

🌐
Desy
hasyweb.desy.de › services › computing › python › node52.html
Class variables
Next: Multiple constructors Up: Class definition Previous: Class definition Contents Index · #!/usr/bin/env python3 class myClass: # # Variables declared inside the class definition, # but not inside a method are class or static variables: # i = 7 def __init__( self): print( "__init__(): self.i %d " % self.i) print( "__init__(): myClass.i %d " % myClass.i) return def main(): o = myClass() print( "main()") print( " myClass.i %d" % myClass.i) print( " o.i %d" % o.i) # # assigning the instance variable does not change the class variable # o.i = 8 print( " >> o.i = 8") print( " myClass.i %d" % my
🌐
DEV Community
dev.to › ogwurujohnson › distinguishing-instance-variables-from-class-variables-in-python-81
Python Class Variables: Distinguishing Instance Variables from Class Variables in python - DEV Community
November 29, 2017 - Now looking at our (Employee) class in the previous example any data that has to be shared by all instances of the Employee class has to be stored in a class variable so all the instances created can have access to it.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-class-members
Python Class Members - GeeksforGeeks
To understand Class members, let ... work: Instance variables in python are such that when defined in the parent class of the object are instantiated and separately maintained for each and every object instance of the class...
Published: April 17, 2023
🌐
Medium
medium.com › data-science › practical-python-class-vs-instance-variables-431fd16430d
Practical Python: Class vs Instance Variables | by Soner Yıldırım | TDS Archive | Medium
November 16, 2020 - Instance is an object that belongs to a class. For instance, list is a class in Python. When we create a list, we have an instance of the list class. In this article, I will focus on class and instance variables. I assume you have a basic knowledge about classes in Python.
🌐
TechGeekBuzz
techgeekbuzz.com › home › blog › python › python class variables with examples
Python Class Variables with Examples
Class variables are those variables that are defined inside the class and class methods, but outside the instance method without using the self keyword. The value of the class variables does not change object to object, it remains the same for ...
🌐
ITU Online
ituonline.com › blogs › python-class-variables
Python Class Variables: Declaration, Usage, and Practical Examples – ITU Online IT Training
August 3, 2023 - ... A class variable is an attribute defined on the class rather than inside an individual object, which means every instance can read the same value unless it is overridden by an instance attribute.
🌐
DEV Community
dev.to › ankitmalikg › difference-bw-class-variable-and-instance-variable-python-100o
Python - Difference b/w class variable and instance variable - DEV Community
August 31, 2024 - They are defined outside of any method or constructor and are associated with the class itself, not with any particular instance. To declare a class variable, you simply assign a value to it inside the class definition, using the = operator.
🌐
YouTube
youtube.com › watch
Python Class Variables - YouTube
Class Variables in Python. Class Variables are variables shared by all instances of a class. This differs from an instance variable, also known as an attribu...
Published: March 10, 2025
🌐
Squash
squash.io › how-to-use-class-and-instance-variables-in-python
How to Use Class And Instance Variables in Python
The wheels class variable is set to 4, representing the number of wheels for all cars. The fuel_type class variable is set to "gasoline", representing the common fuel type for all cars. To define instance variables in Python, you can assign values to them within the class methods, typically within the __init__ method.