python - 在python中获取函数名称作为字符串

标签 python function introspection

<分区>

Possible Duplicate:
How do I get the name of a function or method from within a Python function or method?
How to get the function name as string in Python?

我有一个名为func 的函数,我希望能够将函数名称作为字符串获取。

伪 python :

def func () :
    pass

print name(func)

这将打印“func”。

最佳答案

这很简单。

print func.__name__

编辑:但你必须小心:

>>> def func():
...     pass
... 
>>> new_func = func
>>> print func.__name__
func
>>> print new_func.__name__
func

关于python - 在python中获取函数名称作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7142062/

相关文章:

python - 枚举在 Python 2.7 中不可迭代

python - 二维点云中的线检测

python - 将 Scrapy 与 Django 集成 : How to

javascript - 是否可以说 Node 编译器暂时忽略严格模式?

javascript - 如何重构使用两次的 JavaScript 函数

exception - 如何访问 `clojure.lang.Compiler$CompilerException`实例的行变量?

python - 如何限制使用 random.choice() 选择列表项的次数

r - 如何在列表上应用 "composed"函数

ios - 在 Objective-C 中使用 KVC 读取联合属性

perl - 枚举 perl 解释器中的所有引用