The behavior of an object in Python is determined by its type (type). The so-called type is support for certain operations. Numeric objects are basic elements in any programming language and support math operations such as add, subtract, multiply, and divide.
Python's numeric objects have integers and floating-point numbers, and support various mathematical operations such as +,-,*,/etc. Without these operators, the program can only use function calls to perform mathematical operations, such as Add (2, 3), sub (5, 2).
The function of the operator in the program is consistent with the usage of ordinary mathematical operation, which is more simple and intuitive to use. In Python, these operator implementations are implemented by defining some special methods of object, such as object.__add__ () and object.__sub__ (). If you implement these special methods when you define a class, you can simulate the behavior of a digital object by enabling the object of the custom class to support the corresponding mathematical operation. This actually achieves the effect of operator overloading.
This is accomplished by implementing a Chinese number class with support for addition operations, which shows how to implement a common mathematical operation in Python. The basic definition of the Chinesenumber class is as follows.
Class Chinesenumber: def __init__ (self, N): self.num = n self.alphabet = [u ' 0 ', U ' one ', U ' II ', U ' three ', U ' four ', u ' Five ', U ' vi ', U ' seven ', U ' eight ', U ' IX ', U ' Ten ', def __str__ (self): sign = ' negative ' if Self.num < 0 Else ' return sign + '. J Oin ([Self.alphabet[int (s)] for S in STR (ABS (SELF.NUM))]) def __repr__ (self): return self.__str__ ()
At present, the effect of the implementation is this:
>>> a = Chinesenumber (2) >>> a #调用a. __repr__ () two >>> print (a) #调用a. __str__ () II
General mathematical operators
When defining a class, implement the __add__ () method to add a + operator to the class. Add the following method to Chinesenumber:
def __add__ (self, Other): if Type (other) is Chinesenumber: return Chinesenumber (Self.num + other.num) elif Type (other) was int: return Chinesenumber (Self.num + other) else: return notimplemented
The Chinesenumber object can then use +.
>>> A, B = Chinesenumber (2), Chinesenumber (Ten) >>> A + b 12 >>> A + 57 >>> A + 3.7TypeErro r:unsupported operand type (s) for +: ' chinesenumber ' and ' float '
For +,a + B is equivalent to calling a.__add__ (b). Similarly, you can define other mathematical operators, as shown in the following table.
Object.__add__ (self, Other): +object.__sub__ (Self, Other):-object.__mul__ (Self, Other): *object.__matmul__ (Self, Other): @object. __truediv__ (self, Other):/object.__floordiv__ (Self, Other)://object.__mod__ (Self, Other):%object._ _divmod__ (self, Other): Divmod, Divmod (A, B) = (A/b, a%b) object.__pow__ (self, Other[,modulo]): * *, pow () Object.__lshift_ _ (Self, Other): < >object.__and__ (self, Other):
&object.__xor__ (self, Other): ^object.__or__ ( Self, Other): |
Mathematical operator for operand reversal (reflected/swapped Operand)
>>> 2 + atypeerror:unsupported operand type (s) for +: ' int ' and ' chinesenumber '
2 is an integer type, and its __add__ () method does not support objects of the Chinesenumber class, so the above error occurs. The mathematical operator that defines the inverse of the operand can solve the problem. Add the __radd__ () method to the Chinesenumber class to achieve the + operation of the inverse of the operand.
def __radd__ (self, Other): return self.__add__ (Other)
For a + B, if a does not define the __add__ () method, Python attempts to invoke the __radd__ () method of B. At this point, a + B is equivalent to calling b.__radd__ (a).
>>> a = Chinesenumber (2) >>> 2 + a four
Similarly, you can define mathematical operators for other operand reversals, as shown in the following table.
Object.__radd__ (self, Other): +object.__rsub__ (Self, Other):-object.__rmul__ (Self, Other): *object.__rmatmul__ (self , other): @object. __rtruediv__ (self, Other):/object.__rfloordiv__ (Self, Other)://object.__rmod__ (Self, Other):%o Bject.__rdivmod__ (self, Other): Divmod, Divmod (A, b) = (b/a, b%a) object.__rpow__ (self, Other[,modulo]): * *, POW () object. __rlshift__ (self, Other): < >object.__rand__ (self, Other): &object.__rxor__ (self, other
): ^ Object.__ror__ (self, Other): |
Operation Assignment operator
The operation assignment operator uses a single operator to complete operations and assignment operations, such as a + = B equivalent to calling a = a + B. To add the __iadd__ () method to Chinesenumber, you can implement the + = operator.
def __iadd__ (self, Other): if Type (other) is Chinesenumber: self.num + = Other.num return self elif type (other) is int: Self.num + = other return self else: return notimplemented
At this time
>>> A, B = Chinesenumber (2), Chinesenumber (Ten) >>> A + = b>>> a 12 >>> A + 7>>> a Nineteen
Similarly, you can define additional operation assignment operators, as described in the following table.
Object.__iadd__ (self, Other): +=object.__isub__ (Self, Other):-=object.__imul__ (Self, Other): *=object.__imatmul__ ( Self, Other): @=object.__itruediv__ (Self, Other):/=object.__ifloordiv__ (Self, Other)://=object.__imod__ (self, other ):%=object.__ipow__ (self, Other[,modulo]): **=object.__ilshift__ (Self, Other): <<=object.__irshift__ (Self, Other): >>=object.__iand__ (Self, Other): &=object.__ixor__ (Self, Other): ^=object.__ior__ (Self, Other): |=
Unary math operator
A unary mathematical operator is an operation that has only one operand, such as a negative number operator. -the corresponding special function is __neg__ (). Add the __neg__ () method for Chinesenumber,
def __neg__ (self): return Chinesenumber (-self.num)
At this point, the Chinesenumber object is supported-operation.
>>> a = Chinesenumber (5) >>>-a minus five
The other unary operators are shown in the following table.