Day 8: OOPS in Python, Class Attributes vs Instance Attributes and Static Methods


In Object-Oriented Programming (OOP), attributes are used to store data related to a class or an instance of that class. Python provides two types of attribute.
1. Class Attributes (Shared by all instances)
2. Instance Attributes (Unique to each object)
Class Attributes
Class attributes are shared across all instances of a class.
They are defined inside the class but outside any method.
Changing a class attribute affects all instances unless overridden by an instance.
Instance Attributes
Instance attributes are unique to each object.
They are defined inside the constructor (__init__()).
Changing an instance attribute affects only that specific object.
Instance attributes has higher precedence over class attributes.
#Class attr vs instance attr
class Student:
college_name = "xyz_university" #class attribute, syntax: classname.attr
#parameterized constructor
def __init__(self, name, id, marks):
self.name = name
self.id = id
self.marks = marks
s1 = Student("Rishitha", 101, 97)
print(s1.name, s1.id, s1.marks) #s1.name - instance attribute, syntax: obj.attr
s2 = Student("Sravya", 102, 89)
print(s2.name, s2.id, s2.marks)
print(Student.college_name)
#output
#Rishitha 101 97
#Sravya 102 89
#xyz_university
Methods
Methods are functions that belong to objects
Class has data (attributes) and methods.
#Create a student class that takes name and marks of 3 subjects as arguments in constructor.
#Then create a method to print the average.
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def calc_avg_of_marks(self):
total = 0
for val in self.marks:
total += val
avg = total/len(self.marks)
return avg
s1 = Student("Student1", [89, 78, 56])
print("Avg marks of Student1 is: ", s1.calc_avg_of_marks())
s2 = Student("Student2", [98, 91, 67])
print("Avg marks of Student2 is: ", s2.calc_avg_of_marks())
#output:
#Avg marks of Student1 is: 74.33333333333333
#Avg marks of Student2 is: 85.33333333333333
Static Methods
A static method in Python is a method that belongs to a class rather than an instance. Unlike instance methods, it does not receive the instance (self) or class (cls) as the first parameter.
Static methods are defined using the @staticmethod decorator and are used when a method does not need access to instance-specific data or class-level attributes.
class MathOperations:
@staticmethod
def add(x, y):
return x + y
# Calling the static method
result = MathOperations.add(10, 5)
print(result) # Output: 15
Method Type | Uses self ? | Uses cls ? | Can Access Instance Attributes? | Can Access Class Attributes? |
Instance Method | ✅ Yes | ❌ No | ✅ Yes | ✅ Yes |
Class Method | ❌ No | ✅ Yes | ❌ No | ✅ Yes |
Static Method | ❌ No | ❌ No | ❌ No | ❌ No |
class Example:
def instance_method(self):
return "Instance Method: Can access instance attributes."
@classmethod
def class_method(cls):
return "Class Method: Can access class attributes."
@staticmethod
def static_method():
return "Static Method: Cannot access instance or class attributes."
# Creating an instance of the class
obj = Example()
# Calling methods
print(obj.instance_method()) # Requires an instance
print(Example.class_method()) # Can be called on the class
print(Example.static_method()) # Can be called on the class
#output
#Instance Method: Can access instance attributes.
#Class Method: Can access class attributes.
#Static Method: Cannot access instance or class attributes.
Subscribe to my newsletter
Read articles from Rishitha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
