Python Class Variables
Table of Contents
In the last lesson, we learned about Python Instance Variables. After learning about instance variables, let us learn about class variables in Python. Class variables are variables that belong to the class, not to any specific object.
In this tutorial, we will learn what class variables are, how to use them, and how they differ from instance variables.
What Are Class Variables in Python?
Class variables are variables that are shared by all objects of a class. They belong to the class itself, not to any specific object.
We define them inside the class but outside any method.
class Dog: species = "Canis familiaris"
def __init__(self, name): self.name = name
dog1 = Dog("Buddy")dog2 = Dog("Max")
print(dog1.species)print(dog2.species)print(Dog.species)Output
Canis familiarisCanis familiarisCanis familiarisHere, species is a class variable. It is shared by all dogs. And we can access it through the class or through any object.
Why Do We Need Class Variables in Python?
Class variables are useful when we want to share data across all objects of a class.
For example, if all dogs belong to the same species, we do not need to store the species in each dog. We can store it once in the class.
That saves memory. And it makes our code cleaner.
How to Create Class Variables in Python
We create class variables by assigning a value inside the class, but outside any method.
class Dog: species = "Canis familiaris" count = 0
def __init__(self, name): self.name = name Dog.count += 1
dog1 = Dog("Buddy")dog2 = Dog("Max")
print(Dog.count)Output
2Here, count is a class variable. Each time we create a dog, we increase the count. And all dogs share the same count.
How to Access Class Variables in Python
We can access class variables through the class or through an object.
class Dog: species = "Canis familiaris"
def __init__(self, name): self.name = name
dog = Dog("Buddy")
print(Dog.species)print(dog.species)Output
Canis familiarisCanis familiarisBoth work. But it is better to use the class name when we want to be clear that it is a class variable.
How to Modify Class Variables in Python
We can modify class variables through the class.
class Dog: species = "Canis familiaris"
Dog.species = "Canis lupus"print(Dog.species)Output
Canis lupusBut if we try to modify through an object, it creates an instance variable instead.
class Dog: species = "Canis familiaris"
dog = Dog()dog.species = "Canis lupus"
print(dog.species)print(Dog.species)Output
Canis lupusCanis familiarisSee? The object got its own species attribute. The class variable was not changed.
That is an important difference. Be careful.
A Real-Life Example β Employee Class
Letβs create an employee class with a class variable for the company name.
class Employee: company = "TechCorp" count = 0
def __init__(self, name, salary): self.name = name self.salary = salary Employee.count += 1
def introduce(self): print(f"Hi, I am {self.name}. I work at {Employee.company}.")
emp1 = Employee("Alex", 50000)emp2 = Employee("Sam", 60000)
emp1.introduce()emp2.introduce()print(f"Total employees: {Employee.count}")Output
Hi, I am Alex. I work at TechCorp.Hi, I am Sam. I work at TechCorp.Total employees: 2The company name is shared by all employees. The count is also shared. That is the power of class variables.
Instance Variables vs Class Variables in Python
Instance variables belong to each object. Class variables belong to the class.
| Instance Variables | Class Variables |
|---|---|
| Unique to each object | Shared by all objects |
Defined in __init__ with self | Defined in class body |
Accessed with self or object name | Accessed with class name |
Example: self.name | Example: species |
Use instance variables when each object needs its own data. Use class variables when data is shared across all objects.
Common Mistakes with Class Variables in Python
Mistake 1: Modifying class variables through objects
class Dog: species = "Canis familiaris"
dog = Dog()dog.species = "Canis lupus"
print(Dog.species)Output
Canis familiarisThe class variable was not changed. The object got its own instance variable.
Fix: Modify through the class.
Dog.species = "Canis lupus"Mistake 2: Using mutable class variables
class Dog: tricks = []
dog1 = Dog()dog2 = Dog()
dog1.tricks.append("sit")print(dog2.tricks)Output
['sit']Because the list is shared. Both dogs see the same list.
Fix: Use instance variables for mutable data.
class Dog: def __init__(self): self.tricks = []Mistake 3: Forgetting to use the class name
class Dog: count = 0
def __init__(self): count += 1This gives an error. Because count is not defined locally.
Fix:
class Dog: count = 0
def __init__(self): Dog.count += 1Next, we will learn about Python Methods and define actions that work with object data.