Python 运算符重载中的反向方法
1、前提
print(type(NotImplemented)) # 特殊单值,是NotImplementedType的实例
print(type(None)) # 特殊单值
print(type(NotImplementedError)) # Exception 类
<class 'NotImplementedType'>
<class 'NoneType'>
<class 'type'>
2、__add__
和 __iadd__
# 结果返回的是实例的时候,才会调用 repr str 或者 bytes
class A:
def __init__(self, x):
self.x = x
def __add__(self, other):
print('add ~~~~~')
return self.x + other.x
def __iadd__(self, other):
print('iadd ~~~~~')
return A(self.x + other.x)
def __radd__(self, other):
print('radd ~~~~~')
return self.x + other.x
def