📜  在Python中模拟数字类型

📅  最后修改于: 2022-05-13 01:54:30.202000             🧑  作者: Mango

在Python中模拟数字类型

以下是可以定义为模拟数字类型对象的函数。

在相同类型的对象上实现二进制操作的方法:

这些函数不会对调用对象进行任何更改,而是在对调用对象执行某些操作后返回一个相同类型的新数字对象。以下是实现算术二进制运算的方法。

MethodOperation
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)

__pow__() 被定义为接受第三个可选参数,以支持 pow()函数的三元版本。此外,如果上述任何方法不支持该操作,它应该返回NotImplemented

在不同类型的对象上实现二进制操作的方法:

如果左侧的对象类型(可收集对象)不同,则可以使用以下方法执行算术二进制运算:

MethodOperation
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)

例如,如果在 a.__sub__(b) 中,a 与 b 一样不是数字类型,那么此方法将返回NotImplemented,然后要执行 a – b,我们将调用 a.__rsub__(b)。

实现算术赋值操作的方法:

这些方法用于实现算术赋值操作。它们不会返回新对象,而是会在调用对象本身时分配新值。就像x.__imul__(y)将被执行为x = x * y 。以下是每种方法的对应操作。

MethodOperation
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)

实现一元算术运算的方法:

以下是实现一元算术运算的方法,例如数字的负数、数字的倒数等。

MethodOperation
object.__neg__(self) (unary minus)
object.__pos__(self)+ (unary plus)
object.__abs__(self)abs() in-built function
object.__invert__(self)~ (complement of a number)

其他一些重要的方法:

MethodDescription
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.