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____concat__Python には特別なメソッドはありません。

于 2010-03-08T10:24:51.107 に答える