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.

Answer from Mike Mรผller on Stack Overflow
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python object-oriented programming (oop) โ€บ python instance variables explained with examples
Python Instance Variables With Examples โ€“ PYnative
October 21, 2021 - We can access the instance variable using the object and dot (.) operator. In Python, to work with an instance variable and method, we use the self keyword. We use the self keyword as the first parameter to a method.
๐ŸŒ
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

Two ways to declare instance variables in Python? - Stack Overflow
Is there any difference in functionality and what happens behind the scenes between class Class: def __init__(self, var1): self.var1 = var1 and class Class: var1 = "" def __in... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
Declaring an instance variable at the class level
Deleted in response to Spataner's link to PEP-526 . More on reddit.com
๐ŸŒ r/learnpython
10
2
August 5, 2024
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
๐ŸŒ
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.
๐ŸŒ
Medium
medium.com โ€บ @SarmithaKrishnagobal โ€บ understanding-class-and-instance-variables-in-python-2ed8d363e7ce
Understanding Class and Instance Variables in Python | by Sarmitha Krishnagobal | Medium
January 5, 2024 - Instance Variables: Unique Data for Each Instance Definition: Instance variables are declared within a method (usually the __init__ constructor) and belong to a specific instance of the class.
Top answer
1 of 2
13

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.

2 of 2
5

As I pointed out in comment, the Class.var1 is a shared variable. It could be accessed for each instance as self.var1 if you don't erase it with a non-shared variable in __init__. Here is a sample of what happens :

class Test(object):
    shared = 0
    def __init__(self, i):
        self.notshared = i
    def intro(self):
        print("Shared is " + str(self.shared))
        print("NotShared is " + str(self.notshared))

test1 = Test(4)

test1.intro()
# Shared is 0
# NotShared is 4

Test.shared = 5
test1.intro()
# Shared is 5
# NotShared is 4

test2 = Test(3)
test2.intro()
# Shared is 5
# NotShared is 3

Be aware however that if you write the self.shared variable afterwards, you do not access the shared variable with self.shared anymore:

test1.shared = 12
test1.intro()
# Shared is 12
# NotShared is 4
test2.intro()
# Shared is 5
# NotShared is 3
๐ŸŒ
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 - To declare an instance variable, you need to define it inside a method or constructor of the class. Here's an example: class MyClass: def __init__(self, instance_variable): self.instance_variable = instance_variable obj1 = MyClass("obj1 instance variable") print(obj1.instance_variable) # Output: obj1 instance variable obj2 = MyClass("obj2 instance variable") print(obj2.instance_variable) # Output: obj2 instance variable
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ different-ways-to-access-instance-variable-in-python
Different ways to access Instance Variable in Python - GeeksforGeeks
July 12, 2025 - There are two ways to access the instance variable of class: Within the class by using self and object reference. ... #creating class class student: # constructor def __init__(self, name, rollno): # instance variable self.name = name self.rollno = rollno def display(self): # using self to access # variable inside class print("hello my name is:", self.name) print("my roll number is:", self.rollno) # Driver Code # object created s = student('HARRY', 1001) # function call through object s.display() # accessing variable from # outside the class print(s.name)
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.
๐ŸŒ
Dremendo
dremendo.com โ€บ python-programming-tutorial โ€บ python-instance-variables
Instance Variables in Python Programming | Dremendo
We use Instance Variables to store values in an object. Each object has its own copy of instance variables that are not shared between other objects.
๐ŸŒ
YouTube
youtube.com โ€บ watch
What Is An Instance Variable In Python - YouTube
In this python tutorial, I answer the question of what is an instance variable in python! I give you the definition of an instance variable as well as provid...
Published: February 27, 2023
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python class variables vs. instance variables
Python Class Variables vs. Instance Variables | Career Karma
December 1, 2023 - Python instance variables are owned by an instance of a class. The value of an instance variable can be different depending on the instance with which the variable is associated. This means that the value of each instance variable can be. This is unlike a class variable where the variable can only have one value that you assign. Instance variables are declared inside a class method. Here is an example of two instance variables in Python:
๐ŸŒ
DEV Community
dev.to โ€บ shameel โ€บ instance-variables-and-instance-methods-in-python-51o0
Instance Variables and Instance Methods in Python - DEV Community
February 20, 2026 - An instance variable is a variable that belongs to a specific object. ... Now email belongs only to user1. If we check user2, it still does NOT have email until we set it. That is why instance variables are object-specific.
๐ŸŒ
W3Resource
w3resource.com โ€บ python-interview โ€บ what-are-instance-variables-in-python-classes.php
What are instance variables in Python classes?
In Python classes, instance variables are variables that are unique to each instance (object) of the class. They are defined within the class and used to store data specific to individual objects.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python oop โ€บ python instance variables
Python Instance Variables
March 31, 2025 - Each instance has its own __dict__ attribute and the keys in this __dict__ may be different. When you access a variable via the instance, Python finds the variable in the __dict__ attribute of the instance.
๐ŸŒ
Medium
harish303.medium.com โ€บ understanding-instance-variables-in-python-made-simple-3315a5976d5e
Understanding Instance Variables in Python (Made Simple!) | by Harish Anbalagan | Medium
March 5, 2025 - Each object created from a class has its own separate copy of these variables. Unlike class variables, which are shared across all instances, instance variables store unique data for each object. Think of it like this: If you and I both enroll in an online Python course, we each have our own login credentials.
๐ŸŒ
Just Academy
justacademy.co โ€บ blog-detail โ€บ what-is-instance-variable-in-python
What is Instance Variable in Python by Roshan Chaturvedi | JustAcademy
May 7, 2024 - In Python, an instance variable is a variable that is defined inside a class and belongs to a specific instance (or object) of that class. Each object can have its own set of instance variables that store data unique to that object.
๐ŸŒ
Python Basics
python-basics-tutorial.readthedocs.io โ€บ en โ€บ latest โ€บ oop โ€บ variables.html
Variables - Python Basics - Read the Docs
In the previous example, length is an instance variable of Square instances, which means that each instance of the class Square has its own copy of length, and the value stored in this copy may be different from the values stored in the length variable in other instances. In Python, you can create instance variables as needed by assigning them to the field of a class instance.
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-instance-variables
Python Instance Variables: A Comprehensive Guide - CodeRivers
February 22, 2026 - This blog post will explore the fundamental concepts of Python instance variables, their usage methods, common practices, and best practices. ... An instance variable in Python is a variable that is associated with a specific instance of a class. Each object (instance) of a class has its own ...