Open In App

Emulating Numeric types in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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.

Method Operation
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.__divmod__(self, other) divmod()
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 (callable object) is different than following methods can be used to perform arithmetic binary operations :

Method Operation
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.__rdivmod__(self, other) divmod()
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.

Method Operation
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.

Method Operation
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 :

Method Description
object.__index__(self)

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 

under __index__().

object.__round__(self, ndigits)

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.

Last Updated : 27 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads