Opening Hours :7AM to 9PM
You can change the meaning of an operator in Python depending upon the operands used. In this tutorial, you will learn how to use operator overloading in Python Object Oriented Programming.
class Point: def __init__(self, x=0, y=0): self.x = x self.y = y p1 = Point(1, 2) p2 = Point(2, 3) print(p1+p2) Run Code Output Traceback (most recent call last): File "<string>", line 9, in <module> print(p1+p2) TypeError: unsupported operand type(s) for +: 'Point' and 'Point'Here, we can see that a TypeError was raised, since Python didn't know how to add two Point objects together.
>>> p1 = Point(2,3) >>> print(p1) <__main__.Point object at 0x00000000031F8CC0>Suppose we want the print() function to print the coordinates of the Point object instead of what we got. We can define a __str__() method in our class that controls how the object gets printed. Let's look at how we can achieve this:
class Point: def __init__(self, x = 0, y = 0): self.x = x self.y = y def __str__(self): return "({0},{1})".format(self.x,self.y) Now let's try the print() function again. class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __str__(self): return "({0}, {1})".format(self.x, self.y) p1 = Point(2, 3) print(p1) Run Code Output (2, 3) That's better. Turns out, that this same method is invoked when we use the built-in function str() or format(). >>> str(p1) '(2,3)' >>> format(p1) '(2,3)'So, when you use str(p1) or format(p1), Python internally calls the p1.__str__() method. Hence the name, special functions. Now let's go back to operator overloading.
class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __str__(self): return "({0},{1})".format(self.x, self.y) def __add__(self, other): x = self.x + other.x y = self.y + other.y return Point(x, y) Now let's try the addition operation again: class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __str__(self): return "({0},{1})".format(self.x, self.y) def __add__(self, other): x = self.x + other.x y = self.y + other.y return Point(x, y) p1 = Point(1, 2) p2 = Point(2, 3) print(p1+p2) Run Code Output (3,5)What actually happens is that, when you use p1 + p2, Python calls p1.__add__(p2) which in turn is Point.__add__(p1,p2). After this, the addition operation is carried out the way we specified.
Operator | Expression | Internally |
---|---|---|
Addition | p1 + p2 | p1.__add__(p2) | Subtraction | p1 - p2 | p1.__sub__(p2) |
Multiplication | p1 * p2 | p1.__mul__(p2) |
Power | p1 ** p2 | p1.__pow__(p2) |
Division | p1 / p2 | p1.__truediv__(p2) |
Floor Division | p1 // p2 | p1.__floordiv__(p2) |
Remainder (modulo) | p1 % p2 | p1.__mod__(p2) |
Bitwise Left Shift | p1 << p2 | p1.__lshift__(p2) |
Bitwise Right Shift | p1 >> p2 | p1.__rshift__(p2) |
Bitwise AND | p1 & p2 | p1.__and__(p2) |
Bitwise OR | p1 | p2 | p1.__or__(p2) |
Bitwise CAP | p1 ^ p2 | p1.__xor__(p2) |
Bitwise NOT | ~p1 | p1.__invert__() |
class Point: def __init__(self, x=0, y=0): self.x = x self.y = y def __str__(self): return "({0},{1})".format(self.x, self.y) def __lt__(self, other): self_mag = (self.x ** 2) + (self.y ** 2) other_mag = (other.x ** 2) + (other.y ** 2) return self_mag < other_mag p1 = Point(1,1) p2 = Point(-2,-3) p3 = Point(1,-1) # use less than print(p1<p2) print(p2<p3) print(p1<p3) Run Code Output True False FalseSimilarly, the special functions that we need to implement, to overload other comparison operators are tabulated below.
Operator | Expression | Internally |
---|---|---|
Less than | p1 < p2 | p1.__lt__(p2) | Less than or equal to | p1 <= p2 | p1.__le__(p2) |
Equal to | p1 == p2 | p1.__eq__(p2) |
Not equal to | p1 != p2 | p1.__ne__(p2) |
Greater than | p1 > p2 | p1.__gt__(p2) |
Greater than or equal to | p1 >= p2 | p1.__ge__(p2) |