Operator overloading in Python allows you to define custom behaviors for standard operators (+, -, *, etc.) for user-defined classes. This makes objects of your classes interact more intuitively with built-in operators.
__) that correspond to operators.Here are some commonly used magic methods for operator overloading:
__add__(self, other): Defines behavior for + (addition).__sub__(self, other): Defines behavior for - (subtraction).__mul__(self, other): Defines behavior for * (multiplication).__truediv__(self, other): Defines behavior for / (division).__eq__(self, other): Defines behavior for == (equality).__lt__(self, other): Defines behavior for < (less than).__gt__(self, other): Defines behavior for > (greater than).Vector Classclass Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vector(self.x - other.x, self.y - other.y)
def __mul__(self, scalar):
return Vector(self.x * scalar, self.y * scalar)
def __str__(self):
return f"Vector({self.x}, {self.y})"
# Usage
v1 = Vector(2, 3)
v2 = Vector(4, 5)
v3 = v1 + v2
v4 = v2 - v1
v5 = v1 * 3
print(v3) # Output: Vector(6, 8)
print(v4) # Output: Vector(2, 2)
print(v5) # Output: Vector(6, 9)
__init__): Initializes vector coordinates x and y.__add__): Returns a new Vector object by adding corresponding components.__sub__): Returns a new Vector object by subtracting corresponding components.__mul__): Multiplies both components by a scalar value.__str__): Provides a readable string representation of the vector.class ComplexNumber:
def __init__(self, real, imag):
self.real = real
self.imag = imag
def __add__(self, other):
return ComplexNumber(self.real + other.real, self.imag + other.imag)
def __str__(self):
return f"{self.real} + {self.imag}i"
# Usage
c1 = ComplexNumber(1, 2)
c2 = ComplexNumber(3, 4)
c3 = c1 + c2
print(c3) # Output: 4 + 6i
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def __eq__(self, other):
return self.area() == other.area()
def __lt__(self, other):
return self.area() < other.area()
# Usage
r1 = Rectangle(2, 3)
r2 = Rectangle(3, 4)
r3 = Rectangle(2, 3)
print(r1 == r2) # Output: False
print(r1 < r2) # Output: True
print(r1 == r3) # Output: True