【问题标题】:Overload () operator in PythonPython中的重载()运算符
【发布时间】:2013-03-21 02:16:25
【问题描述】:

我正在尝试为我的班级学习 Python 中的柯里化,我必须为它重载 () 运算符。但是,我不明白如何才能重载 () 运算符。你能解释一下重载括号背后的逻辑吗?我应该先超载(然后)还是可以做任何这些?另外,括号运算符有特殊名称吗?

【问题讨论】:

标签: python operator-overloading


【解决方案1】:

您可以通过实现__call__ 方法使对象可调用:

class FunctionLike(object):
    def __call__(self, a):
        print("I got called with {!r}!".format(a))

fn = FunctionLike()
fn(10)

# --> I got called with 10!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-03-05
  • 2010-12-28
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
相关资源
最近更新 更多