There are too many magic methods related to operators. J will roughly list the following two categories:
1. Comparison operators
##__cmp__(self, other)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. __eq__(self, other)Defines the behavior of the comparison operator ==__ne__(self, other )Defines the behavior of comparison operator !=__lt__(self, other) Defines the behavior of comparison operator <__gt__(self, other)Defines the behavior of the comparison operator>__le__(self, other) Defines the behavior of the comparison operator <= __ge__(self, other) Defines the behavior of the comparison operator >=You can understand it by looking at a simple example:
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- class Number(object): def __init__(self, value): self.value = value def __eq__(self, other): print('__eq__') return self.value == other.value def __ne__(self, other): print('__ne__') return self.value != other.value def __lt__(self, other): print('__lt__') return self.value < other.value def __gt__(self, other): print('__gt__') return self.value > other.value def __le__(self, other): print('__le__') return self.value <= other.value def __ge__(self, other): print('__ge__') return self.value >= other.value if __name__ == '__main__': num1 = Number(2) num2 = Number(3) print('num1 == num2 ? --------> {} \n'.format(num1 == num2)) print('num1 != num2 ? --------> {} \n'.format(num1 == num2)) print('num1 < num2 ? --------> {} \n'.format(num1 < num2)) print('num1 > num2 ? --------> {} \n'.format(num1 > num2)) print('num1 <= num2 ? --------> {} \n'.format(num1 <= num2)) print('num1 >= num2 ? --------> {} \n'.format(num1 >= num2))
The output result is:
__eq__ num1 == num2 ? --------> False __eq__ num1 != num2 ? --------> False __lt__ num1 < num2 ? --------> True __gt__ num1 > num2 ? --------> False __le__ num1 <= num2 ? --------> True __ge__ num1 >= num2 ? --------> False
2. Arithmetic operator
Magic method | Description |
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>> |