Python magic method-single-eye and General Arithmetic Operations, python magic
In the comparison of magic methods, we have discussed that magic methods actually overload operators, such as >,<, =. Here, we will continue to discuss the magic methods for numerical values.
1. Single Object operator or single object operation function
_ Pos _ (self)
Implement a positive number operation (for example, + some_object, python calls the _ pos _ function)
_ Neg _ (self)
Implement a negative number operation (such as-some_object)
_ Abs _ (self)
Implement the behavior of a built-in abs () function
_ Invert _ (self)
Implement an inverse operator (~ Operator.
_ Round _ (self, n)
Implement the behavior of a built-in round () function. N is the decimal number to be rounded up. (It seems to be discarded in 2.7 or another version)
_ Floor _ (self)
Implement math. floor () function behavior. For example, place the number to the nearest integer. (It seems to be discarded in 2.7 or other new versions)
_ Ceil _ (self)
Implement math. ceil () function behavior. For example, you can integer the number to the nearest integer (it seems to be discarded in 2.7 or other new versions)
_ Trunc _ (self)
Implement the math. trunc () function, for example, truncating a number to obtain an integer.
class Foo(str): def __new__(cls, x, *args, **kwargs): return super(Foo, cls).__new__(cls, x) def __init__(self, x): self.x = x def __pos__(self): return '+' + self.x def __neg__(self): return '-' + self.x def __abs__(self): return 'abs:' + self.x def __invert__(self): return 'invert:' + self.xa = Foo('scolia')print +aprint -aprint ~a
2. General Arithmetic Operations
_ Add _ (self, other)
Implement an addition.
_ Sub _ (self, other)
Implements a subtraction.
_ Mul _ (self, other)
Implement a multiplication.
_ Floordiv _ (self, other)
Implements the division operation produced by a "//" Operator
_ Div _ (self, other)
Implement a division operation represented by the "/" operator. (because the division in Python 3 is changed to true division by default, __div _ does not exist in Python3)
_ Truediv _ (self, other)
Implement real division. Note that it is valid only when you use from _ future _ import division.
_ Mod _ (self, other)
Implements a modulo operation represented by the "%" operator.
_ Divmod _ (self, other)
Implement a built-in function divmod ()
_ Pow __
Implement the behavior of an exponential operation ("**" Operator)
_ Lshift _ (self, other)
Implement a single-digit left-shift operation (<)
_ Rshift _ (self, other)
Implement the one-bit right shift operation (>) function.
_ And _ (self, other)
Implements a bitwise behavior and operation.
_ Or _ (self, other)
Implements a bitwise OR operation (|.
_ Xor _ (self, other)
Implements an exclusive or operation (^ ).
class Foo(str): def __new__(cls, x, *args, **kwargs): return super(Foo, cls).__new__(cls, x) def __init__(self, x): self.x = x def __add__(self, other): return self.x + '+' + other.x def __sub__(self, other): return self.x + '-' + other.x def __mul__(self, other): return self.x + '*' + other.x def __floordiv__(self, other): return self.x + '//' + other.x def __div__(self, other): return self.x + '/' + other.x def __truediv__(self, other): return self.x + 't/' + other.x def __mod__(self, other): return self.x + '%' + other.x def __divmod__(self, other): return self.x + 'divmod' + other.x def __pow__(self, power, modulo=None): return self.x + '**' + str(power) def __lshift__(self, other): return self.x + '<<' + other.x def __rshift__(self, other): return self.x + '>>' + other.x def __and__(self, other): return self.x + '&' + other.x def __or__(self, other): return self.x + '|' + other.x def __xor__(self, other): return self.x + '^' + other.xa = Foo('scolia')b = Foo('good')print a + bprint a - bprint a * bprint a // bprint a / bprint a % bprint divmod(a, b)print a ** bprint a << bprint a >> bprint a & bprint a | bprint a ^ b
from __future__ import division.......print a / b
Welcome to our discussion
Reference: click here