2010-03-08 53 views
1

我试图执行__concat__,但没有奏效实施__concat__在Python

>>> class lHolder(): 
...  def __init__(self,l): 
...    self.l=l 
...  def __concat__(self, l2): 
...    return self.l+l2 
...  def __iter__(self): 
...    return self.l.__iter__() 
... 
>>> lHolder([1])+[2] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: unsupported operand type(s) for +: 'lHolder' and 'list' 

我该如何解决这个问题?

回答

5

__concat__不是一种特殊的方法(​​)。它是运营商模块的一部分。

您需要实施__add__才能获得您想要的行为。

2

要实施__add__而不是__concat__。 Python中没有__concat__特殊方法。