The following are the functions which can be defined to emulate numeric type objects.
Methods to implement Binary operations on same type of objects :
These functions will make no change in the calling object rather they will return a new numeric object of same type after performing certain operation on the calling object. Following are the methods to implement arithmetic binary operations.
|object.__add__(self, other)||+ (Addition)|
|object.__sub__(self, other)||– (Subtraction)|
|object.__mul__(self, other)||* (Multiplication)|
|object.__matmul__(self, other)||@ (Matrix multiplication)|
|object.__truediv__(self, other)||/ (True division)|
|object.__floordiv__(self, other)||// (Floor division)|
|object.__mod__(self, other)||% (Modulus or remainder)|
|object.__pow__(self, other[, modulo])||** (power)|
|object.__lshift__(self, other)||<< (Bit wise left shift)|
|object.__rshift__(self, other)||>> (Bit wise right shift)|
|object.__and__(self, other)||& (Bit wise AND operation)|
|object.__xor__(self, other)||^ (Exclusive OR operation)|
|object.__or__(self, other)||| (Bit wise OR operation)|
The __pow__() is defined to accept a third optional argument so as to support the ternary version of pow() function. Also if any of above method does not support the operation it should return NotImplemented .
Methods to implement Binary operations on different type of objects :
If type of object at left (collable object) is different then following methods can be used to perform arithmetic binary operations :
|object.__radd__(self, other)||+ (Addition)|
|object.__rsub__(self, other)||– (Subtraction)|
|object.__rmul__(self, other)||* (Multiplication)|
|object.__rmatmul__(self, other)||@ (Matrix multiplication)|
|object.__rtruediv__(self, other)||/ (True division)|
|object.__rfloordiv__(self, other)||// (Floor division)|
|object.__rmod__(self, other)||% (Modulus or remainder)|
|object.__rpow__(self, other[, modulo])||** (pow() or power of number)|
|object.__rlshift__(self, other)||<< (Bit wise left shift)|
|object.__rrshift__(self, other)||>> (Bit wise right shift)|
|object.__rand__(self, other)||& (Bit wise AND operation)|
|object.__rxor__(self, other)||^ (Exclusive OR operation)|
|object.__ror__(self, other)||| (Bit wise OR operation)|
For example, if in a.__sub__(b), a is not of numeric type as of b then this method will return NotImplemented, then to perform a – b we will call a.__rsub__(b).
Methods to implement arithmetic assignment operations :
These methods are used to implement the arithmetic assignment operations. They will not return a new object rather they will assign the new value in calling object itself. Like x.__imul__(y) will be performed as x = x * y. Following are the corresponding operations to each method.
|object.__iadd__(self, other)||+= (Addition assignment)|
|object.__isub__(self, other)||-= (Subtraction assignment)|
|object.__imul__(self, other)||*= (Multiplication assignment)|
|object.__imatmul__(self, other)||@= (Matrix multiplication assignment)|
|object.__itruediv__(self, other)||/= (True division assignment)|
|object.__ifloordiv__(self, other)||//= (Floor division assignment)|
|object.__imod__(self, other)||%= (Modulus or remainder assignment)|
|object.__ipow__(self, other[, modulo])||**= (power of number assignment)|
|object.__ilshift__(self, other)||<<= (Bit wise left shift assignment)|
|object.__irshift__(self, other)||>>= (Bit wise right shift assignment)|
|object.__iand__(self, other)||&= (Bit wise AND operation assignment)|
|object.__ixor__(self, other)||^= (Exclusive OR operation assignment)|
|object.__ior__(self, other)|||= (Bit wise OR operation assignment)|
Methods to implement unary arithmetic operations :
Following are the methods to implement unary arithmetic operations like, negative of a number, inverse of a number etc.
|object.__neg__(self)||– (unary minus)|
|object.__pos__(self)||+ (unary plus)|
|object.__abs__(self)||abs() in-built function|
|object.__invert__(self)||~ (complement of a number)|
Some other important methods :
Called to implement operator.index() function, also used to convert a numeric type object to integer type,
or we can say if __int__(), __float__() or __complex__() is not defined then int(), float() and complex() falls
To implement the round() function, the second optional argument tells up to how many decimal places
we want to round the numeric value.
|object.__trunc__(self)||To implement trunc() function.|
|object.__floor__(self)||To implement floor() function.|
|object.__ceil__(self)||To implement ceil() function.|
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Python Regex to extract maximum numeric value from a string
- Sort numeric strings in a list in Python
- Python | Ways to remove numeric digits from given string
- Python | Add only numeric values present in a list
- Python | Get the numeric prefix of given string
- Python | Check if given string is numeric or not
- Python | Check Numeric Suffix in String
- Python | Get numeric prefix of given string
- Python | Convert numeric String to integers in mixed List
- Python - Maximum Pair Summation in numeric String
- Python - Numeric Sort in Mixed Pair String List
- Python | Sort alternate numeric and alphabet list
- Python | Convert numeric words to numbers
- Python program to Count Uppercase, Lowercase, special character and numeric values using Regex
- Python - Extract String till Numeric
- Python - Extract Tuples with all Numeric Strings
- Python - Split Numeric String into K digit integers
- Python - Sort given list of strings by part the numeric part of string
- Numeric fields in serializers - Django REST Framework
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.