Python中的Overload()运算符 [英] Overload () operator in Python
本文介绍了Python中的Overload()运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为我的课程学习Python中的currying,并且必须为此重载()运算符.但是,我不知道如何才能重载()运算符.您能解释一下括号重载背后的逻辑吗?我应该先重载,然后再重载吗?另外,圆括号运算符是否有特殊名称?
I am trying to learn currying in Python for my class and I have to overload the () operator for it. However, I do not understand how can I can go about overloading the () operator. Can you explain the logic behind overloading the parentheses? Should I overload first ( and then ) or can I do any of these? Also, is there special name for parentheses operator?
推荐答案
您可以通过实现__call__
方法来使对象可调用:
You can make an object callable by implementing the __call__
method:
class FunctionLike(object):
def __call__(self, a):
print("I got called with {!r}!".format(a))
fn = FunctionLike()
fn(10)
# --> I got called with 10!
这篇关于Python中的Overload()运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文