在 Python 中实现 __concat__ [英] Implementing __concat__ in Python
本文介绍了在 Python 中实现 __concat__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试实现__concat__
,但没有成功
我该如何解决这个问题?
解决方案
__concat__
并不是一个特殊的方法 (http://docs.python.org/glossary.html#term-special-method).它是操作员模块的一部分.
您需要实现 __add__
以获得您想要的行为.
I tried to implement __concat__
, but it didn't work
>>> 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'
How can I fix this?
解决方案
__concat__
is not a special method (http://docs.python.org/glossary.html#term-special-method). It is part of the operator module.
You will need to implement __add__
to get the behaviour you want.
这篇关于在 Python 中实现 __concat__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文