試圖瞭解__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' and 'X'
爲什麼這不起作用?我在這裏做錯了什麼?
只是想看看__radd__作品。我知道我可以使用__add__ – Tim 2010-11-28 18:31:42