2

我试图实施__concat__,但没有奏效

>>> 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'

我怎样才能解决这个问题?

4

2 回答 2

5

__concat__不是特殊方法(http://docs.python.org/glossary.html#term-special-method)。它是操作员模块的一部分。

您将需要实施__add__以获得您想要的行为。

于 2010-03-08T10:25:28.380 回答
2

你要执行__add__,不是__concat__。Python 中没有__concat__特殊的方法。

于 2010-03-08T10:24:51.107 回答