我想要一个类似的东西
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/