Python Operator Overloading
We know that an operator is a symbol that performs some action. For example, '+' is an operator that performs addition operation when used on numbers. When an operator can perform different actions, it is said to exhibit polymorphism.
Operator Overloading Examples
#Using + operator on objects
class BookX:
def __init__(self, pages):
self.pages = pages
class BookY:
def __init__(self, pages):
self.pages = pages
b1 = BookX(100)
b2 = BookY(150)
print('Total pages=', b1+b2)
Operator Overloading with same class objects
#Using + operator on objects
class BookX:
def __init__(self, pages):
self.pages = pages
class BookY:
def __init__(self, pages):
self.pages = pages
b1 = BookX(100)
b2 = BookY(150)
print('Total pages=', b1+b2)
We can overload the '+' operator to act upon the two objects and perform addition operation on the contents of the objects. That means we are giving additional task to the '+' operator.
Operator Overloading with different class objects
#overloading + operator to act on objects
class BookX:
def __init__(self, pages):
self.pages = pages
def __add__(self, other):
return self.pages+other.pages
class BookY:
def __init__(self, pages):
self.pages = pages
b1 = BookX(100)
b2 = BookY(150)
print('Total pages=', b1+b2)
Following Table summarizes important operators and their corresponding internal methods that can be overridden to act on objects. These methods are called magic methods.
Operator | Magic method |
---|---|
+ | object._add_(self,other) |
- | object._sub_(self,other) |
* | object._mul_(self,other) |
$S | object._div_(self,other) |
$S$S | object._floordiv_(self,other) |
-- | object._mod_(self,other) |
** | object._pow_(self,other[,modulo]) |
+= | object._iadd_(self,other) |
-= | object._isub_(self,other) |
*=() | object._imul_(self,other) |
$S= | object._idiv_(self,other) |
$S$S= | object._ifloordiv_(self,other) |
!-- | object._imod_(self,other[,modulo]) |
**= | object._ipow_(self,other) |
< | object._lt_(self,other) |
<= | object._le_(self,other) |
> | object._gt_(self,other) |
>= | object._ge_(self,other) |
== | object._eq_(self,other) |
!= | object._ne_(self,other) |
If we want to overload the greater than (>) operator. For this purpose the magic method __gt__() should be overridden. For example,
def __gt__(self, other):
return self.pages>other.pages
greater than(>) operator overload
#overloading > operator
class Ramayan:
def __init__(self, pages):
self.pages = pages
def __gt__(self, other):
return self.pages>other.pages
class Mahabharat:
def __init__(self, pages):
self.pages = pages
b1 = Ramayan(1000)
b2 = Mahabharat(1500)
if(b1>b2):
print('Ramayan has more pages')
else:
print('Mahabharat has more pages')
Multiplication(*) operator overload
#overloading the * operator
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def __mul__(self, other):
return self.salary*other.days
class Attendance:
def __init__(self, name, days):
self.name = name
self.days = days
x1 = Employee('Srinu', 500.00)
x2 = Attendance('Srinu', 25)
print('This month salary=', x1*x2)