【问题标题】:Name of the function which has been passed as a parameter [duplicate]作为参数传递的函数的名称[重复]
【发布时间】:2016-11-04 03:57:36
【问题描述】:

我有一个将函数作为参数的类。我需要根据之前传递的函数调用一个函数。有办法吗?

例如,如果myfunc()已经传给类,我需要调用myfunc1(),因为它是对应的函数(我已经保存了函数之间的关系)。

【问题讨论】:

  • 有一个类似的问题heregetting caller method name in called method。
  • 能否包含示例代码?

标签: python class


【解决方案1】:

要获取函数的名称,请使用__name__,例如:

def something(func):
    print(func.__name__)

def myfunc(): pass
something(myfunc) # prints 'myfunc'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 2013-06-01
    • 2014-06-04
    相关资源
    最近更新 更多