Instance Variable vs Class Variable in Python: Key Differences and Usage
instance variable is unique to each object created from a class, storing data specific to that object, while a class variable is shared across all instances of the class, holding data common to all objects. Instance variables are defined inside methods like __init__, whereas class variables are defined directly inside the class but outside any method.Quick Comparison
This table summarizes the main differences between instance variables and class variables in Python.
| Aspect | Instance Variable | Class Variable |
|---|---|---|
| Definition | Variable unique to each object | Variable shared by all objects of the class |
| Where Defined | Inside methods (usually __init__) | Directly inside the class, outside methods |
| Storage | Stored in each object's __dict__ | Stored in the class's __dict__ |
| Access | Accessed via self.variable | Accessed via ClassName.variable or self.variable |
| Effect of Change | Changes affect only that object | Changes affect all instances sharing it |
| Use Case | Store data unique to each object | Store data common to all objects |
Key Differences
Instance variables belong to individual objects created from a class. Each object has its own copy, so changing an instance variable in one object does not affect others. They are usually set inside the __init__ method using self, which refers to the current object.
Class variables, on the other hand, belong to the class itself and are shared by all instances. They are defined directly inside the class but outside any method. If you change a class variable, the change is reflected across all instances unless an instance overrides it by defining its own variable with the same name.
In summary, use instance variables for data unique to each object and class variables for data shared by all objects of that class.
Instance Variable Example
This example shows how instance variables store unique data for each object.
class Dog: def __init__(self, name, age): self.name = name # instance variable self.age = age # instance variable # Create two Dog objects dog1 = Dog('Buddy', 3) dog2 = Dog('Lucy', 5) print(dog1.name, dog1.age) # Buddy 3 print(dog2.name, dog2.age) # Lucy 5
Class Variable Equivalent
This example shows how a class variable is shared across all instances.
class Dog: species = 'Canis familiaris' # class variable def __init__(self, name, age): self.name = name # instance variable self.age = age # instance variable # Create two Dog objects dog1 = Dog('Buddy', 3) dog2 = Dog('Lucy', 5) print(dog1.species) # Canis familiaris print(dog2.species) # Canis familiaris # Change class variable Dog.species = 'Canis lupus' print(dog1.species) # Canis lupus print(dog2.species) # Canis lupus
When to Use Which
Choose instance variables when you need to store information unique to each object, like a person's name or age. Use class variables when you want to share data across all instances, such as a common species name or a default setting. Remember, changing a class variable affects all objects, while changing an instance variable affects only that specific object.