python - 如何在函数内获取函数对象(Python)

标签 python function python-2.7 introspection

我想要一个类似的东西

def x():
  print get_def_name()

但不一定知道x的名字。

理想情况下,它会返回 'x',其中 x 是函数的名称。

最佳答案

您可以使用 Python 的内置检查库来执行此操作。

如果你想处理更复杂的情况,你可以阅读更多它的文档,但这个片段适合你:

from inspect import getframeinfo, currentframe

def test_func_name():
    return getframeinfo(currentframe()).function

print(test_func_name())

关于python - 如何在函数内获取函数对象(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24250118/

相关文章:

python - 如何在 Python 中将表示二进制分数的字符串转换为数字

python - 根据索引选择固定数据帧范围并附加到新数据帧

r - R中的函数 "else"

function - 如何在mathematica中定义通用函数?

python 2.7和3.3子进程模块区别

python - Mac 上的 wxpython - 按钮样式

python - 无法执行 Python Pandas set_value

python - 同一生成器函数的不同调用中的最后一位小数位精度变化[python]

python和子目录的动态导入

python - 通过字典访问函数