Understanding Class and Instance Attributes in Python.
In Python, attributes are variables that belong to a class or an object. There are two main types of attributes:
Understanding the difference between them is very important in Object-Oriented Programming (OOP).
A class attribute is shared by all objects (instances) of a class.
It is defined inside the class but outside methods.
class ClassName:
class_attribute = value
class Student:
school = "Ghazi University" # Class Attribute
# Creating objects
s1 = Student()
s2 = Student()
print(s1.school)
print(s2.school)
Ghazi University
Ghazi University
school is a class attribute.You can access class attributes using:
print(s1.school)
print(Student.school)
class Student:
school = "Ghazi University"
Student.school = "Punjab University"
s1 = Student()
print(s1.school)
Punjab University
An instance attribute belongs to a specific object.
Each object has its own copy of instance attributes.
They are usually created inside the __init__() method using self.
class Student:
def __init__(self, name, roll_no):
self.name = name
self.roll_no = roll_no
# Creating objects
s1 = Student("Ali", 101)
s2 = Student("Sara", 102)
print(s1.name)
print(s2.name)
Ali
Sara
name and roll_no are instance attributes.class Student:
def __init__(self, name):
self.name = name
s1 = Student("Ali")
s2 = Student("Sara")
s1.name = "Ahmed"
print(s1.name)
print(s2.name)
Ahmed
Sara
| Feature | Class Attribute | Instance Attribute |
|---|---|---|
| Shared or Separate | Shared by all objects | Separate for each object |
| Defined | Inside class | Inside methods using self |
| Memory Usage | One copy | Separate copy for each object |
| Accessed By | Class name or object | Object only |
| Purpose | Common data | Object-specific data |
class Student:
school = "Ghazi University" # Class Attribute
def __init__(self, name):
self.name = name # Instance Attribute
s1 = Student("Ali")
s2 = Student("Sara")
print(s1.school)
print(s2.school)
print(s1.name)
print(s2.name)
Ghazi University
Ghazi University
Ali
Sara
Python first checks:
class Student:
school = "Ghazi University"
s1 = Student()
print(s1.school)
s1.school = "UET"
print(s1.school)
print(Student.school)
Ghazi University
UET
Ghazi University
s1.school uses the class attribute.s1.class Car:
wheels = 4 # Class Attribute
def __init__(self, color):
self.color = color # Instance Attribute
c1 = Car("Red")
c2 = Car("Black")
print(c1.wheels)
print(c2.wheels)
print(c1.color)
print(c2.color)
class Test:
value = []
t1 = Test()
t2 = Test()
t1.value.append(10)
print(t2.value)
[10]
Because value is a class attribute shared by all objects.
Use:
| Concept | Description |
|---|---|
| Class Attribute | Shared by all objects |
| Instance Attribute | Unique for each object |
self.attribute |
Creates instance attributes |
ClassName.attribute |
Accesses class attributes |
Create a Teacher class with:
schoolname, subjectCreate two objects and display values.
Create a Mobile class with:
brandmodelChange the class attribute and observe the result.
Class and instance attributes are fundamental concepts in Python OOP.