小编Tim*_*Tim的帖子

为什么__radd__无效

您好

试图了解如何__radd__运作.我有代码

>>> class X(object):
    def __init__(self, x):
        self.x = x
    def __radd__(self, other):
        return X(self.x + other.x)


>>> a = X(5)
>>> b = X(10)
>>> a + b

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    a + b
TypeError: unsupported operand type(s) for +: 'X' and 'X'
>>> b + a

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    b + a
TypeError: unsupported operand type(s) for +: 'X' …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
2
解决办法
6619
查看次数

标签 统计

python ×1