Object- Oriented Programming (OOP) in
Python
Topic: Work with Magic Methods ( __init__, __str__) and
Operator Overloading
Objective:
To understand Python’s magic methods (dunder methods) and how to use them for
operator overloading and custom object behavior.
1. Introduction to Magic Methods
Magic methods (also known as dunder methods, short for “double underscore”) are
special built- in methods that begin and end with __.
They allow customization of how objects behave with operators, functions, and built-
in features.
Common magic methods:
Method Purpose
__init__() Initializes object attributes (constructor)
Defines how an object is represented
__str__() as a string
__add__() Defines behavior of + operator
__sub__() Defines behavior of - operator
__mul__() Defines behavior of * operator
__eq__() Defines equality ( == ) between objects
2. Example Programs
Example 1: Using __init__() and __str__()
class Student:
def __init__(self, name, marks):
[Link] = name
[Link] = marks
def __str__(self):
return f"Student Name: {[Link]}, Marks: {[Link]}"
# Creating object
s1 = Student("Aditya", 90)
print(s1)
Output:
Student Name: Aditya, Marks: 90
Here:
__init__() initializes the object.
__str__() defines how the object is displayed when printed.
Example 2: Operator Overloading (+ Operator)
You can redefine how Python operators work for user- defined classes.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Point({self.x}, {self.y})"
# Creating objects
p1 = Point(3, 5)
p2 = Point(2, 4)
p3 = p1 + p2 # Operator overloading
print(p3)
Output:
Point(5, 9)
Here:
The + operator is redefined to add the coordinates of two Point objects.
Example 3: Overloading Multiple Operators
class Number:
def __init__(self, value):
[Link] = value
def __add__(self, other):
return Number([Link] + [Link])
def __sub__(self, other):
return Number([Link] - [Link])
def __mul__(self, other):
return Number([Link] * [Link])
def __str__(self):
return str([Link])
n1 = Number(10)
n2 = Number(5)
print("Addition:", n1 + n2)
print("Subtraction:", n1 - n2)
print("Multiplication:", n1 * n2)
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Example 4: Comparing Objects (== Operator)
class Book:
def __init__(self, title, pages):
[Link] = title
[Link] = pages
def __eq__(self, other):
return [Link] == [Link]
b1 = Book("Book A", 200)
b2 = Book("Book B", 200)
b3 = Book("Book C", 150)
print(b1 == b2) # True
print(b1 == b3) # False
Output:
True
False
Example 5: Custom Representation with __repr__()
class Employee:
def __init__(self, name, salary):
[Link] = name
[Link] = salary
def __repr__(self):
return f"Employee(name='{[Link]}', salary={[Link]})"
emp = Employee("Ravi", 50000)
print(emp)
Output:
Employee(name='Ravi', salary=50000)
Conclusion:
Magic methods let you customize class behavior to act like built- in types.
__init__() and __str__() define object creation and display.
Operator overloading allows user- defined objects to use operators like +, - , *,
and == .
These concepts make your classes more powerful, readable, and Pythonic.
Top of Form
Bottom of Form