In Python, class attributes are variables that belong to the class itself rather than individual objects. They are shared across all instances (objects) of the class.
Class attributes are useful when you want to store common data that should be the same for every object.
In this tutorial, you will learn what class attributes are, how to use them, and how they differ from instance attributes.
What is a Class Attribute?
A class attribute is a variable defined inside a class but outside any method.
It is shared by all objects of that class.
Creating a Class Attribute
Example:
class Student:
school = "ABC School" # Class attribute
print(Student.school)Accessing Class Attributes
You can access class attributes using:
- Class name
- Object name
Example:
class Student:
school = "ABC School"
s1 = Student()
print(Student.school)
print(s1.school)Class Attribute Shared by All Objects
Example:
class Student:
school = "ABC School"
s1 = Student()
s2 = Student()
print(s1.school)
print(s2.school)Output:
ABC School
ABC SchoolModifying Class Attributes
You should modify class attributes using the class name.
Example:
class Student:
school = "ABC School"
Student.school = "XYZ School"
print(Student.school)Class Attribute vs Instance Attribute
| Class Attribute | Instance Attribute |
|---|---|
| Shared by all objects | Unique to each object |
| Defined in class body | Defined inside init |
| Access via class or object | Access via self |
Example: Both Together
class Student:
school = "ABC School" # Class attribute
def __init__(self, name):
self.name = name # Instance attribute
s1 = Student("John")
s2 = Student("Alice")
print(s1.school, s1.name)
print(s2.school, s2.name)Changing Class Attribute via Object (Important Concept)
If you change class attribute using an object, it creates a new instance variable.
Example:
class Student:
school = "ABC School"
s1 = Student()
s2 = Student()
s1.school = "New School"
print(s1.school)
print(s2.school)Output:
New School
ABC SchoolWhy This Happens
Student.school→ class attributes1.school = ...→ creates instance attribute
Real-World Example: School System
class Student:
school = "Green Valley School"
def __init__(self, name):
self.name = name
s1 = Student("John")
s2 = Student("Sara")
print(s1.name, s1.school)
print(s2.name, s2.school)Real-World Example: Company System
class Employee:
company = "Tech Corp"
def __init__(self, name, role):
self.name = name
self.role = role
e1 = Employee("Ali", "Developer")
e2 = Employee("Sara", "Designer")
print(e1.name, e1.company)
print(e2.name, e2.company)When to Use Class Attributes
Use class attributes when:
- Data is shared by all objects
- Values are constant or common
- Example: school name, company name, tax rate
Common Mistakes
Mistake 1: Modifying via object
❌ Wrong:
s1.school = "New School"✔ Creates instance attribute instead of changing class attribute
Mistake 2: Confusing instance and class attributes
✔ Instance = unique data
✔ Class = shared data
Mistake 3: Forgetting class name access
Student.school = "New Name"✔ Correct way to update class attribute
Safe Example
class Demo:
value = 10
Demo.value = 20
print(Demo.value)Key Points Summary
- Class attributes are shared across all objects
- Defined inside class but outside methods
- Accessed using class name or object
- Changing via class affects all objects
- Changing via object creates instance attribute
Conclusion
Class attributes are an important part of Python OOP that help you manage shared data efficiently.
You learned:
- What class attributes are
- How to create and access them
- Difference between class and instance attributes
- Real-world examples
Mastering class attributes helps you build cleaner and more efficient object-oriented programs.


0 Comments