Python实现__concat__
-
18-09-2019 - |
题
我试图执行__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'
我怎样才能解决这个问题?
解决方案
__concat__
不是特殊方法( HTTP://docs.python。组织/ glossary.html#术语-特殊方法)。它是运营商模块的一部分。
您将需要实现__add__
得到你想要的行为。
其他提示
您想实现__add__
,不__concat__
。有在Python没有__concat__
特殊方法。
不隶属于 StackOverflow