Instance vs Class Variables in Python is an important topic because it explains how data belongs either to each individual object or to the class as a whole. This distinction affects object behavior, memory sharing, design clarity, and some of the most common beginner mistakes in object-oriented Python.
When developers first learn classes, it is easy to store all values in one place without thinking carefully about whether those values should be different for every object or shared across all objects. Instance variables and class variables solve those two different needs, but only if they are used with the right intent.
To use them properly, you need to understand where each kind of variable is defined, how they are accessed, how Python resolves them through the object and class, what happens when names overlap, and why mutable shared class state can create bugs if it is used without care.
What Are Instance Variables in Python?
Instance variables belong to a specific object. Each object gets its own copy of those values, so different instances can hold different state even when they come from the same class.
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
s1 = Student("Ava", 91)
s2 = Student("Riya", 88)
print(s1.name)
print(s2.name)
Here name and marks are instance variables because they are stored on self. Each object has its own version of those attributes.
What Are Class Variables in Python?
Class variables belong to the class itself rather than to a particular object. They are shared by all instances unless an instance creates an overriding attribute of the same name.
class Student:
school = "Nerds Academy"
print(Student.school)
A class variable is useful when the value represents something common to all objects of that type, such as a company name, tax rate, category label, or shared configuration constant.
Where Instance Variables Are Defined
Instance variables are usually defined inside the constructor using self.attribute. This makes them part of the object state from the moment the object is initialized.
Because they live on the object, they can vary between instances and reflect the specific identity or condition of that object.
Where Class Variables Are Defined
Class variables are usually defined directly inside the class body, outside methods. That placement signals that the value belongs to the class-level design rather than to one object instance.
class Car:
wheels = 4
This makes the value accessible through the class itself and through instances unless shadowed by an instance attribute of the same name.
Accessing Instance and Class Variables
Instance variables are accessed through an object, such as s1.name. Class variables can be accessed through the class name, such as Student.school, and they can also often be reached through an instance if not shadowed.
class Student:
school = "Nerds Academy"
def __init__(self, name):
self.name = name
s1 = Student("Ava")
print(s1.name)
print(s1.school)
print(Student.school)
Although accessing class variables through instances works, accessing them through the class name is often clearer because it communicates that the value is shared.
The Main Difference in Meaning
The real difference is semantic. Instance variables represent per-object state. Class variables represent shared class-level state. If the data should vary per object, it belongs in an instance variable. If the data should be common to all objects, it may belong in a class variable.
This question of meaning matters more than the syntax. Correct design starts by asking whether the information is individual or shared.
Shadowing a Class Variable with an Instance Variable
If an instance is assigned an attribute with the same name as a class variable, the instance attribute shadows the class variable for that object.
class Student:
school = "Nerds Academy"
s1 = Student()
s2 = Student()
s1.school = "Private Academy"
print(s1.school)
print(s2.school)
print(Student.school)
This behavior is important because it explains why changing an attribute through one object does not always change the class-level value. Sometimes it only creates a new instance-specific attribute instead.
Changing a Class Variable
If you change a class variable through the class itself, the shared class-level value changes for all instances that still rely on that class attribute.
class Student:
school = "Nerds Academy"
Student.school = "Mega Academy"
print(Student.school)
This is a key difference from instance variable assignment. Updating through the class affects the shared attribute. Updating through the object may create an instance-level shadow instead.
Mutable Class Variables Need Care
One of the biggest practical dangers is using mutable class variables such as lists or dictionaries when you really wanted each object to have its own separate container. Because class variables are shared, every instance will observe the same underlying object.
class Team:
members = []
If one object modifies that shared list, all instances see the change. This is sometimes intentional, but very often it is an unintended bug. In many cases, mutable per-instance data belongs in the constructor as an instance variable instead.
When Class Variables Are a Good Fit
Class variables are a good fit for shared constants, counters, default labels, version markers, and settings that genuinely belong to the class itself rather than to one object. They can also be useful when the class should track information shared across instances, such as how many objects have been created.
Used well, class variables make class-level meaning explicit instead of hiding shared state in random module globals.
When Instance Variables Are a Good Fit
Instance variables are the correct choice when the value is part of the unique state of each object. Names, balances, marks, positions, configuration chosen per object, and collected per-instance data almost always belong here.
This is the normal default for object state in most practical Python class design.
Instance vs Class Variables in Real Programs
In real programs, instance variables are used for object-specific records such as user details, device readings, and order data. Class variables are used for shared labels, counters, defaults, and class-level metadata. Understanding the difference helps prevent accidental shared state and improves class readability.
This topic matters because it is one of the first places where object-oriented design decisions become concrete in Python code.
Common Mistakes with Instance and Class Variables
- Using a class variable when the data should belong to each instance separately.
- Creating mutable shared class variables unintentionally.
- Not realizing that assignment through an instance may shadow a class variable instead of changing the shared one.
- Confusing shared configuration with per-object state.
- Accessing class-level values through instances in ways that hide the shared meaning.
Best Practices for Instance and Class Variables
- Use instance variables for per-object state.
- Use class variables only for genuinely shared data.
- Be especially careful with mutable class variables.
- Prefer class-name access for shared variables when clarity matters.
- Think about the meaning of the data before deciding where it belongs.
Instance vs Class Variables in Python Interview Points
For interviews, you should know where each kind of variable is defined, how they are accessed, how shadowing works, why mutable class variables are risky, and how to decide whether a piece of data is per-object or shared across instances.
What is an instance variable in Python?
An instance variable belongs to a specific object and usually stores state unique to that object.
What is a class variable in Python?
A class variable belongs to the class itself and is shared across instances unless shadowed.
What happens if an instance assigns to the same name as a class variable?
The instance usually creates or updates its own attribute, which shadows the class variable for that object.
Why are mutable class variables risky?
Because all instances can share the same underlying mutable object, causing unintended cross-instance state changes.
How Updates Behave in Practice
The real difference becomes clear when values change. Updating an instance variable only affects one object because the value lives in that object namespace. Updating a class variable affects every object that still reads the shared class-level value. This is why class variables are useful for shared configuration, counters, and defaults, but not for object-specific state such as balance, roll number, or sensor reading.
class Student:
school = "Nerds Academy"
def __init__(self, name):
self.name = name
s1 = Student("Ava")
s2 = Student("Noah")
Student.school = "Embedded Lab"
s1.name = "Ava Sharma"
print(s1.school, s1.name)
print(s2.school, s2.name)
A Common Mistake with Mutable Class Variables
One common mistake is storing a list or dictionary as a class variable when each object should have its own copy. In that case, every object starts sharing the same container, and one update unexpectedly changes the data seen by all other objects. If the data belongs to each object separately, define it inside __init__() as an instance variable instead of placing it at the class level.
A simple rule works well: if the value describes the class as a whole, use a class variable; if it describes one object and can vary from object to object, use an instance variable. That rule prevents most beginner mistakes.
Lookup Order in Python Attribute Access
When Python evaluates an expression like obj.value, it first checks the object itself and then moves to the class if the attribute is not found on the instance. This lookup order explains why an instance variable can hide a class variable with the same name. Understanding that search path makes the behavior feel logical instead of surprising.
A practical rule is simple: class variables describe the class, while instance variables describe one object. Once that idea is clear, the syntax becomes much easier to reason about in real code.