There are too many magic methods related to operators. J will roughly list the following two categories:
1. Comparison operators
Magic method | Description |
if This method returns a negative number, indicating self < other; Returns a positive number, indicating self > other; Returns 0, indicating self == other. It is strongly not recommended to define __cmp__. Instead, it is best to define __lt__, __eq__ and other methods separately to implement the comparison function. __cmp__ is deprecated in Python3. | |
Defines the behavior of the comparison operator == | |
Defines the behavior of comparison operator != | |
Defines the behavior of comparison operator < | |
Defines the behavior of the comparison operator> | |
Defines the behavior of the comparison operator <= | |
Defines the behavior of the comparison operator >= |
Magic Method | Instructions |
__add__(self, other) | Implements the addition operation |
__sub__(self, other) | Implements the subtraction operation |
__mul__(self, other) | Implements the multiplication operation |
__floordiv__( self, other) | Implements the // operator |
___div__(self, other) | Implements the / operator. This method is available in Python3 Deprecated. The reason is that in Python3, division defaults to true division |
__truediv__(self, other) | implements true division. Only if you declare from __future__ import division This method will take effect |
__mod__(self, other) | Implements the % operator and remainder operation |
__divmod__(self, other) | Implements the divmod() built-in function |
__pow__(self, other) | Implements the ** operation. Nth power operation |
__lshift__(self, other) | implements bit operation<< |
__rshift__( self, other) | Implements bit operations>> |
Implements bit operations& | |
Implements bit operations` | |
Implemented bit operations^ |