Instance Variables vs Class Variables in Python
In Python, variables in a class can be categorized as Instance Variables and Class Variables. Understanding the difference between these two is crucial for managing object-oriented programming effectively.
What are Instance Variables?
Instance Variables are variables that are unique to each instance (object) of a class. They are defined inside the constructor method __init__()
using self
.
Each instance of the class has its own copy of the instance variable.
What are Class Variables?
Class Variables are shared across all instances of the class. They are declared inside the class but outside any method. Unlike instance variables, class variables maintain the same value for all instances unless modified specifically for an instance.
Examples
1. Basic Example of Instance and Class Variables
In this example, we define a class Car
with an instance variable color
and a class variable wheels
.
Each car object will have its own color, but the number of wheels will be the same for all car instances.
class Car:
# Class variable shared by all instances
wheels = 4
def __init__(self, color):
# Instance variable unique to each instance
self.color = color
# Creating car instances
car1 = Car("Red")
car2 = Car("Blue")
# Accessing instance variables
print("Car1 Color:", car1.color)
print("Car2 Color:", car2.color)
# Accessing class variable
print("Car1 Wheels:", car1.wheels)
print("Car2 Wheels:", car2.wheels)
Output:
Car1 Color: Red
Car2 Color: Blue
Car1 Wheels: 4
Car2 Wheels: 4
Here, color
is an instance variable, meaning each object has its own value.
However, wheels
is a class variable shared across all objects.
2. Modifying Instance vs Class Variables
Let’s see what happens when we modify the instance and class variables.
We will change the color
of car1
and modify wheels
for car1
only.
class Car:
wheels = 4 # Class variable
def __init__(self, color):
self.color = color # Instance variable
car1 = Car("Red")
car2 = Car("Blue")
# Modifying the instance variable for car1
car1.color = "Green"
# Modifying the class variable for car1 only
car1.wheels = 6
print("Car1 Color:", car1.color) # Green
print("Car2 Color:", car2.color) # Blue
print("Car1 Wheels:", car1.wheels) # 6
print("Car2 Wheels:", car2.wheels) # 4
Output:
Car1 Color: Green
Car2 Color: Blue
Car1 Wheels: 6
Car2 Wheels: 4
Notice that changing color
affected only car1
, as it is an instance variable.
However, when we changed wheels
for car1
, it created a new instance variable for car1
rather than modifying the class variable.
3. Properly Modifying Class Variables
If we want to modify the class variable for all instances, we must change it using the class name.
class Car:
wheels = 4 # Class variable
def __init__(self, color):
self.color = color # Instance variable
car1 = Car("Red")
car2 = Car("Blue")
# Modifying the class variable correctly
Car.wheels = 6
print("Car1 Wheels:", car1.wheels) # 6
print("Car2 Wheels:", car2.wheels) # 6
Output:
Car1 Wheels: 6
Car2 Wheels: 6
Here, modifying Car.wheels
changed the value for all instances because wheels
is a class variable.
4. Common Mistake: Confusing Instance and Class Variables
Beginners often mistakenly assume modifying a class variable for one instance affects all instances. Let’s see an example where this mistake happens.
class Car:
wheels = 4 # Class variable
def __init__(self, color):
self.color = color # Instance variable
car1 = Car("Red")
car2 = Car("Blue")
# Modifying the class variable incorrectly through an instance
car1.wheels = 6
print("Car1 Wheels:", car1.wheels) # 6
print("Car2 Wheels:", car2.wheels) # 4
Output:
Car1 Wheels: 6
Car2 Wheels: 4
Since we assigned car1.wheels = 6
, Python created a new instance variable wheels
for car1
rather than modifying the class variable.
To modify the class variable for all instances, use Car.wheels = 6
instead.