Cas*_*ash 1 python sequences operator-overloading
我试图实现__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'
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
__concat__不是一种特殊的方法(http://docs.python.org/glossary.html#term-special-method).它是操作员模块的一部分.
您需要实现__add__以获得所需的行为.