🧭 Pyhton Operator Overloading: When Python Gives “superpowers” To Your Objects!
Let’s start with something simple.
You already know what the “+” sign does, right? When you write:
print(5 + 10)
Python adds the two numbers and gives 15
.
Easy-peasy!
Now, look at this:
print("Hello " + "World")
Python joins the two words and gives:
Hello World
Wait a second! 🤔 How did “+” add numbers and also join words? Isn’t that the same operator?
Yes! But Python is smart.
It knows that +
means addition for numbers, and joining for strings.
That’s what we call operator overloading —
When the same operator behaves differently depending on the type of data it’s working with.
🧩 The Secret Behind It
Every operator in Python (like +
, -
, *
, /
, ==
, <
, etc.) actually calls a special hidden method behind the scenes.
Let’s reveal the magic.
When you write:
5 + 10
Python secretly does this:
(5).__add__(10)
When you write:
"Hello " + "World"
Python actually calls:
"Hello ".__add__("World")
Each data type defines how it wants to behave with that operator.
And the coolest part? You can teach your own classes how to behave with those operators too! 💪
That’s called operator overloading — giving your class objects custom powers to use normal math symbols in cool ways.
🐍 Example: Adding Two Objects
Let’s say we create a simple class to represent points on a graph.
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
Now we create two points:
p1 = Point(2, 3)
p2 = Point(4, 5)
What if we try to add them?
print(p1 + p2)
💥 Python gets confused! It will say something like:
TypeError: unsupported operand type(s) for +: 'Point' and 'Point'
Why? Because Python doesn’t know how to add two Point objects — unless we teach it how.
🧠 Giving It Power — The __add__
Method
Let’s define what the +
should do for our Point
objects.
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"({self.x}, {self.y})"
Now let’s try again:
p1 = Point(2, 3)
p2 = Point(4, 5)
print(p1 + p2)
Output:
(6, 8)
🎉 It works!
The +
operator now adds the coordinates of the two points and gives us a new Point.
We’ve just overloaded the + operator — gave it new meaning for our own objects!
🧮 Other Common Overloaded Operators
Python lets you overload many operators by defining their “magic methods” — methods with double underscores (also called dunder methods).
Operator | Magic Method | Example Usage |
---|---|---|
+ |
__add__ |
a + b |
- |
__sub__ |
a - b |
* |
__mul__ |
a * b |
/ |
__truediv__ |
a / b |
== |
__eq__ |
a == b |
< |
__lt__ |
a < b |
> |
__gt__ |
a > b |
str() |
__str__ |
Converts object to string |
Let’s make another example to see this in action.
🧩 Example: Comparing Two Boxes by Weight
Imagine two boxes with different weights:
class Box:
def __init__(self, weight):
self.weight = weight
def __lt__(self, other):
return self.weight < other.weight
def __str__(self):
return f"Box({self.weight}kg)"
Now:
b1 = Box(10)
b2 = Box(20)
print(b1 < b2)
Output:
True
Python used our custom __lt__
(less than) method!
So b1 < b2
means 10 < 20
— and it’s true.
⚙️ Why It’s So Useful
Operator overloading lets you:
✅ Make your classes act like built-in data types
✅ Write cleaner, more intuitive code
✅ Express mathematical or logical relationships naturally
Instead of writing:
Point.add_points(p1, p2)
you simply write:
p1 + p2
Much more natural, right?
🧠 Remember This
- Every operator calls a special method (like
__add__
,__sub__
, etc.) - You can redefine those methods inside your class.
- You decide what happens when that operator is used.
- It makes your objects smarter, flexible, and more human-like to code with.
🧭 Summary Table
Operator | Method | Description |
---|---|---|
+ |
__add__() |
Adds two objects |
- |
__sub__() |
Subtracts two objects |
* |
__mul__() |
Multiplies two objects |
/ |
__truediv__() |
Divides two objects |
== |
__eq__() |
Checks equality |
< |
__lt__() |
Checks if one object is smaller |
str() |
__str__() |
Converts object to readable string |
✍ Review Fill-in-the-Gap Questions
- Operator overloading means giving an operator new __ for custom objects.
- The
+
operator calls the special method __ behind the scenes. - Operators like
+
,-
, and*
can be given new meanings by defining __ methods. - In the
Point
example, the+
operator adds thex
andy
__ of both points. - Methods with double underscores like
__add__
are called __ methods. - The
__str__()
method allows an object to be displayed as a __. - The operator
<
calls the method __ in a class. - Operator overloading makes objects behave like built-in __ types.
- Overloading the
==
operator uses the method named __. - Operator overloading helps make code more natural and __ to read.