Plus (+) operator is used for adding numbers and at the same time to concatenate strings. It is possible because + operator is overloaded by both int class and str class. Operators are methods, defined in respective classes. Defining methods for operators is known as Operator Overloading. To use + operator with custom objects, implement method __add__(). Special methods defined in Python for numeric operator as given in below table.
Operator | Method |
---|---|
+ | __add__()(self, other) |
– | __sub__()(self, other) |
* | __mul__()(self, other) |
/ | __truediv__()(self, other) |
// | __floordiv__()(self, other) |
% | __mod__()(self, other) |
<< | __lshift__()(self, other) |
>> | __rshift__()(self, other) |
& | __and__()(self, other) |
^ | __xor__()(self, other) |
| | __or__()(self, other) |
These methods are called to implement the binary arithmetic operations. For instance, to evaluate the expression x + y, where x is an instance of a class that has an __add__() method, x.__add__(y) is called. If one of those methods does not support the operation with the supplied arguments, it should return NotImplemented.
In-Place Assignment
Above operator may create a new object which contain the result. Below methods are called to implement the augmented arithmetic assignments. These methods should attempt to do the operation in-place and return the result (which could be, but does not have to be, self). Special methods defined for in-place operator as given in below table.
Operator | Method |
---|---|
+= | __iadd__()(self, other) |
-= | __isub__()(self, other) |
*= | __imul__()(self, other) |
/= | __itruediv__()(self, other) |
//= | __ifloordiv__()(self, other) |
%= | __imod__()(self, other) |
<<= | __ilshift__()(self, other) |
>>= | __irshift__()(self, other) |
&= | __iand__()(self, other) |
^= | __ixor__()(self, other) |
|= | __ior__()(self, other) |
If a specific method is not defined for in-place operator, the augmented assignment falls back to the normal methods.
Example
Below example illustrate the use of various special operators defined above. Point class implement __repr__() methods, along with numerical operators i.e. __add__(), __sub__() and __iadd__(). When the two instance of class Point are added, it internally invokes __add__() implemented by this class. Same holds true for other overloaded operators.
class Point(): def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return "<Point x:{0},y:{1}>".format(self.x, self.y) # Implement Addition def __add__(self, other): return Point(self.x + other.x, self.y + other.y) # Implement Subtraction def __sub__(self, other): return Point(self.x - other.x, self.y - other.y) # implement in-place addition def __iadd__(self, other): self.x += other.x self.y += other.y return self def main(): # Declare points p1 = Point(10, 20) p2 = Point(30, 50) print(p1, p2) # Output # <Point x:10,y:20> <Point x:30,y:50> # Add two points p3 = p1 + p2 print(p3) # Output # <Point x:40,y:70> # Subtract two points p4 = p2 - p1 print(p4) # Output # <Point x:20,y:30> # Perform in-place addition p1 += p2 print(p1) # Output # <Point x:40,y:70> if __name__ == "__main__": main()