Python 如果“\uuu add\uuuu”引发“NotImplementedError”,是否调用“\uu radd”?
假设我们写一个小类:Python 如果“\uuu add\uuuu”引发“NotImplementedError”,是否调用“\uu radd”?,python,python-3.x,operator-overloading,Python,Python 3.x,Operator Overloading,假设我们写一个小类: class K: pass obj = K() 是下面的代码 total = 4 + obj 。。。基本上与以下内容相同 import io try: total = 4.__add__(obj) except NotImplementedError: try: total = obj.__radd__(4) except AttributeError: # type(obj) does not have
class K:
pass
obj = K()
是下面的代码
total = 4 + obj
。。。基本上与以下内容相同
import io
try:
total = 4.__add__(obj)
except NotImplementedError:
try:
total = obj.__radd__(4)
except AttributeError:
# type(obj) does not have an `__radd__` method
with io.StringIO() as string_stream:
print(
"unsupported operand type(s) for +:",
repr(type(4).__name__),
"and",
repr(type(obj).__name__),
file=string_stream
) # `repr` puts quotes around the type names
msg = string_stream.getvalue()
raise TypeError(msg) from None
触发
\uuu radd\uuu()
的行为实际上不是未实现的错误
,而是一个称为未实现
的特殊对象:
>>> help(NotImplemented)
Help on NotImplementedType object:
class NotImplementedType(object)
| Methods defined here:
|
| __reduce__(...)
| Helper for pickle.
|
| __repr__(self, /)
| Return repr(self).
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
NotImplementedError
仍将作为错误传播。但是,返回NotImplemented
对象(而不是引发错误)将允许\uuu radd\uuu()
触发:
>>> class A:
... def __add__(self, other):
... raise NotImplementedError()
...
>>> class B:
... def __add__(self, other):
... print("__add__ was called")
... def __radd__(self, other):
... print("__radd__ was called")
...
>>> class C:
... def __add__(self, other):
... return NotImplemented
...
>>> a, b, c = A(), B(), C()
>>> b + a
__add__ was called
>>> a + b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __add__
NotImplementedError
>>> b + c
__add__ was called
>>> c + b
__radd__ was called
>>A类:
... 定义添加(自身、其他):
... 引发未实现的错误()
...
>>>B类:
... 定义添加(自身、其他):
... print(“\uuuu add\uuuuuuuu被调用”)
... 定义(自身、其他):
... 打印(“\uuuuradd\uuuuuuuuuuuuuuuuuuuuuuuuuuu被称为”)
...
>>>C类:
... 定义添加(自身、其他):
... 返回未执行
...
>>>a,b,c=a(),b(),c()
>>>b+a
__已调用“添加”
>>>a+b
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第3行,在添加中__
未实现错误
>>>b+c
__已调用“添加”
>>>c+b
__radd_uuuuu被称为
触发的行为实际上不是未实现的错误,而是一个称为未实现的特殊对象:
>>> help(NotImplemented)
Help on NotImplementedType object:
class NotImplementedType(object)
| Methods defined here:
|
| __reduce__(...)
| Helper for pickle.
|
| __repr__(self, /)
| Return repr(self).
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
NotImplementedError
仍将作为错误传播。但是,返回NotImplemented
对象(而不是引发错误)将允许\uuu radd\uuu()
触发:
>>> class A:
... def __add__(self, other):
... raise NotImplementedError()
...
>>> class B:
... def __add__(self, other):
... print("__add__ was called")
... def __radd__(self, other):
... print("__radd__ was called")
...
>>> class C:
... def __add__(self, other):
... return NotImplemented
...
>>> a, b, c = A(), B(), C()
>>> b + a
__add__ was called
>>> a + b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __add__
NotImplementedError
>>> b + c
__add__ was called
>>> c + b
__radd__ was called
>>A类:
... 定义添加(自身、其他):
... 引发未实现的错误()
...
>>>B类:
... 定义添加(自身、其他):
... print(“\uuuu add\uuuuuuuu被调用”)
... 定义(自身、其他):
... 打印(“\uuuuradd\uuuuuuuuuuuuuuuuuuuuuuuuuuu被称为”)
...
>>>C类:
... 定义添加(自身、其他):
... 返回未执行
...
>>>a,b,c=a(),b(),c()
>>>b+a
__已调用“添加”
>>>a+b
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第3行,在添加中__
未实现错误
>>>b+c
__已调用“添加”
>>>c+b
__radd_uuuuu被称为