小编dsh*_*han的帖子

__ipow__ 在左侧对象返回 NotImplemented 时引发 TypeError

如果我有两个对象Aand B,我可以返回NotImplementedforA__iadd__方法并使用它的方法进行B修改。A__radd__

>>> class A():
...     def __init__(self, val):
...         self.val = val
...     def __iadd__(self, other):
...         return NotImplemented
...     def __ipow__(self, other):
...         return NotImplemented 
... 
>>> class B():
...     def __init__(self, val):
...         self.val = val
...     def __radd__(self, other):
...         return A(other.val + self.val)
...     def __rpow__(self, other):
...         return A(other.val ** self.val) 
... 
>>> a = A(2)
>>> b = B(2)
>>> …
Run Code Online (Sandbox Code Playgroud)

operators in-place python-3.x

3
推荐指数
1
解决办法
83
查看次数

标签 统计

in-place ×1

operators ×1

python-3.x ×1