0
0
PythonComparisonBeginner · 3 min read

Instance Variable vs Class Variable in Python: Key Differences and Usage

In Python, an 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.

AspectInstance VariableClass Variable
DefinitionVariable unique to each objectVariable shared by all objects of the class
Where DefinedInside methods (usually __init__)Directly inside the class, outside methods
StorageStored in each object's __dict__Stored in the class's __dict__
AccessAccessed via self.variableAccessed via ClassName.variable or self.variable
Effect of ChangeChanges affect only that objectChanges affect all instances sharing it
Use CaseStore data unique to each objectStore 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.

python
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
Output
Buddy 3 Lucy 5
↔️

Class Variable Equivalent

This example shows how a class variable is shared across all instances.

python
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
Output
Canis familiaris Canis familiaris Canis lupus 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.

Key Takeaways

Instance variables hold data unique to each object and are defined with self inside methods.
Class variables hold data shared by all instances and are defined directly inside the class.
Changing a class variable affects all instances, but changing an instance variable affects only one object.
Use instance variables for object-specific data and class variables for common data across objects.