Yes, though c.__class__.a or type(c).a. The two differ slightly in that old-style classes (hopefully, those are all dead by now - but you never know...) have a type() of <type 'instance'> (and __class__ works as expected) while for new-style classes, type() is identical to __class__ except when the object overrides attribute access.

Answer from user395760 on Stack Overflow
๐ŸŒ
iBiblio
ibiblio.org โ€บ swaroopch โ€บ byteofpython โ€บ read โ€บ class-and-object-vars.html
Class and Object Variables
Note that an object variable with the same name as a class variable will hide the class variable! We refer to the object variable name using self.name notation in the methods of that object. Remember this simple difference between class and object variables.
๐ŸŒ
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.
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
python - Object vs. class variable - Stack Overflow
This is a completely theoretical question. Suppose the following code: >>> class C: ... a = 10 ... def f(self): self.a = 999 ... >>> >>> C.a 10 >>> c = ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python Class Variables Vs Instance Variables - Stack Overflow
They are essentially variables that are shared between all instances of a class. Two practical applications of this is are as an instance counter, or as default settings for all objects. If they are changed through the definition itself (Animal.y = 14) they will be updated for all instances and ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
๐ŸŒ
Vskills
vskills.in โ€บ home โ€บ class and object variables
Class and object variables - Tutorial
April 12, 2024 - Overall, class variables and instance variables allow us to define variables in a class that can be shared or unique to each instance of the class. https://www.vskills.in/certification/certified-python-developer
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ difference-between-instance-variable-and-class-variable
Difference between Instance Variable and Class Variable - GeeksforGeeks
July 23, 2025 - They are tied to a particular object instance of the class, therefore, the contents of an instance variable are totally independent of one object instance to others.
๐ŸŒ
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.
๐ŸŒ
IDC Online
idc-online.com โ€บ technical_references โ€บ pdfs โ€บ information_technology โ€บ Class_and_Object_Variables.pdf pdf
CLASS AND OBJECT VARIABLES
Object variables are owned by each individual object/instance of the class. In this ยท case, each object has its own copy of the field i.e. they are not shared and are not ยท related in any way to the field by the same name in a different instance.
Find elsewhere
๐ŸŒ
Kaiyuanba
kaiyuanba.cn โ€บ content โ€บ develop โ€บ byteofpython_html_120 โ€บ byteofpython_html โ€บ class-and-object-vars.html
Class and Object Variables
Note that an object variable with the same name as a class variable will hide the class variable! We refer to the object variable name using self.name notation in the methods of that object. Remember this simple difference between class and object variables.
๐ŸŒ
Shefali
learnify.shefali.dev โ€บ tutorials โ€บ python-instance-vs-class-variables
Python Instance vs Class Variables | Learnify
Changing a class variable affects all objects (unless shadowed by an instance variable). ... class MyClass: class_variable = 0 # Class variable def __init__(self): MyClass.class_variable += 1 def print_class_variable(self): print(MyClass.class_variable) obj1 = MyClass() obj2 = MyClass() obj1.print_class_variable() # Output: 2 obj2.print_class_variable() # Output: 2
Top answer
1 of 3
2

Defining the variables like you did makes them class variables. If you want to guarantee a fresh copy for every instance, you'd do something like

class Animal:
    def __init__(self):
        self.x = {}
        self.y = 0

In your case the definition

class Animal:
    x = {}
    y = 0

means that every instance of the class Animal gets the identical reference to an empty dictionary, there are not multiple dictionaries. If you add to that dictionary, every instance will know. If you wanted to give an instance it's own version, you could technically do animal1.x = {'num': 14}. This way you create a new dictionary and assign it. This won't affect animal2.

2 of 3
1

Class variables can be difficult to understand at first. They are essentially variables that are shared between all instances of a class. Two practical applications of this is are as an instance counter, or as default settings for all objects. If they are changed through the definition itself (Animal.y = 14) they will be updated for all instances and future instances.

This contrasts entirely with instance variables which are always specific to and only accessible through their respective object.

However, the problem with the example you provided is that you are confusing assignment with mutation. You mutated the dictionary whereas you assigned the integer. To assign the dictionary the same way you assigned 14 to the y variable you would have to create a new dictionary like so:

animal1.x = {'num': 14}

This way it will not affect the animal2 object.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ g-fact-34-class-or-static-variables-in-python
Class (Static) and Instance Variables in Python - GeeksforGeeks
Any variable assigned directly inside the class body automatically becomes a class variable. A class variable is a variable that belongs to the class itself rather than to any specific object.
Published: March 6, 2026
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python object-oriented programming (oop) โ€บ python class variables
Python Class Variables With Examples โ€“ PYnative
September 8, 2023 - Value of a class variable is not varied from object to object, and all instances of a class share it. Learn to create, access, and modify the class variables
๐ŸŒ
Kadenfrisk
python.kadenfrisk.com โ€บ python object oriented programming โ€บ classes and objects in python โ€บ instance variables vs class variables
Instance Variables vs Class Variables | Fluffy's Python Course
class Student: school = "Python ... attributes that are unique to each object. Use class variables for attributes and methods that should be shared by all instances of the class....
๐ŸŒ
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
In object-oriented programming, variables at the class level are referred to as class variables, whereas variables at the object level are called instance variables. This differentiation allows us to use class variables to initialize objects with a specific value assigned to variables, and ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ class-objects-vs-variables
Class objects vs. variables
Python is an Object-Oriented Programming Language. A Class is used to create User-Defined Data Structures. ... Object is the unique instance of a class. ... A variable is a container used to store data values.
๐ŸŒ
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 - The main difference between class variables and instance variables is that class variables are shared by all instances of the class, while instance variables are unique to each instance.
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-declaring-a-variable-as-an-object-or-an-instance-when-using-classes-and-objects-in-Python
What is the difference between declaring a variable as an object or an instance when using classes and objects in Python? - Quora
Answer: An instance of a class is also called an object. Python variables are untyped, so to create them you initialize them. If you defined a class X, you create an instance of X by calling X as a function passing in the arguments of the constructor of X (which strangely must be called __init__)...
๐ŸŒ
Coddy.Tech
coddy.tech โ€บ learn โ€บ python โ€บ instance vs class variables
Instance vs Class Variables โ€“ Python | Coddy
January 14, 2026 - Alice Bob A B Python High School ... Difference: Instance variables (created with self.variable_name) are unique to each object, while class variables (defined directly in the class) are shared by all objects....
๐ŸŒ
Career Karma
careerkarma.com โ€บ blog โ€บ python โ€บ python class variables vs. instance variables
Python Class Variables vs. Instance Variables | Career Karma
December 1, 2023 - This shows one of the main differences between instance and class variables in action: instance variables can have different values for each instance of the class, whereas class variables are the same across all instances.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ when to use class variables vs instance variables
r/learnpython on Reddit: When to use class variables vs instance variables
February 9, 2023 -

I thought I understood the use cases for class vs instance variables:

  • Class: when you want the variable to be the same across all objects

  • Instance: when you want the variable to be unique to each object

But I'm working through the Codecademy DSA with Python course, and doing an implementation of a hash map. The hashing function (.hash()) finds the byte-code version of the key string, and sums it for the hash. Why is Codecademy's solution of key_bytes using a class variable, vs I thought an instance variable is more appropriate? Because for each hash map I make, I will want a unique key_byte per object?

class HashMap:
  def __init__(self, array_size):
    self.array_size = array_size
    self.array = [None for item in range(array_size)]
  def hash(self, key):
    #codecademy forces this `key_bytes` as a class variable as solution
    key_bytes = key.encode()
    self.hash_code = sum(key_bytes)

    #why is `key_bytes` not an instance variable:
    self.key_bytes = key.encode()
    self.hash_code = sum(self.key_bytes)