回答
这也适用于:
import sys
def applejuice(q):
func_name = sys._getframe().f_code.co_name
print func_name
你需要解释你的问题是什么。因为回答你的问题是:
print "applejuice"
也许他的意思是:def func(anothah_func):打印anothah_func的名字 – wilhelmtell 2009-10-08 20:27:15
嗯,那绝对有可能。我们会看看他是否说出了问题所在。 – 2009-10-08 20:38:36
def applejuice(**args):
print "Running the function 'applejuice'"
pass
或使用:
myfunc.__name__
>>> print applejuice.__name__
'applejuice'
upvote for'myfunc .__ name__' – philshem 2014-03-24 13:31:08
import traceback
def applejuice(q):
stack = traceback.extract_stack()
(filename, line, procname, text) = stack[-1]
print procname
我想这是用于调试,所以你可能想看看traceback
module提供的其他程序。他们会告诉你打印整个调用堆栈,异常跟踪等
另一种方式
import inspect
def applejuice(q):
print inspect.getframeinfo(inspect.currentframe())[2]
def foo():
# a func can just make a call to itself and fetch the name
funcName = foo.__name__
# print it
print 'Internal: {0}'.format(funcName)
# return it
return funcName
# you can fetch the name externally
fooName = foo.__name__
print 'The name of {0} as fetched: {0}'.format(fooName)
# print what name foo returned in this example
whatIsTheName = foo()
print 'The name foo returned is: {0}'.format(whatIsTheName)
这个网站给了我一个体面的解释sys._getframe.f_code.co_name如何工作,返回函数名称。
http://code.activestate.com/recipes/66062-determining-current-function-name/
Wocome to SO!请考虑提供解释,而不是只指向外部链接。 – 2015-03-19 18:14:43
- 1. 如何在内核API中打印函数指针的函数名称?
- 2. SymPy只打印函数名称
- 3. 在Python中的打印函数中打印函数
- 4. 如何在javascript中打印函数内部的参数值?
- 5. 打印分配给函数指针的函数名称
- 6. Python可以打印函数定义吗?
- 7. python错误打印函数
- 8. 宏从函数内部获取函数名称和参数值
- 9. 如何在函数内部打印变量
- 10. 如何在类成员函数内打印调用者对象名称?
- 11. 如何在python中以表格形式打印字典数据
- 12. excel xll:从函数内部调用函数的名称
- 13. python调用函数内部的函数
- 14. 从函数内部调用Python函数
- 15. 任何方式从内部函数内退出外部函数?
- 16. 如何从函数内部打印变量?
- 17. Python:在打印()函数内打开文件
- 18. 是在Python中打印一个函数?
- 19. 在python shell中打印函数
- 20. 打印具有唯一参数的函数的名称列表
- 21. 如何在python的randrange函数中打印所有随机数
- 22. 如何在python中打印__init__函数参数?
- 23. 在打印变异函数外部函数(Python)时遇到问题
- 24. 检查一个函数在Python中使用打印函数
- 25. Nodejs - 打印函数名称,行号和文件名
- 26. 在我自己的函数中打印变量名称
- 27. Python - 内部函数,闭包和工厂函数 - 如何分解?
- 28. 如何在打印函数时调用
- 29. 如何从azure函数内部访问blob名称?
- 30. 函数名称$
见http://meta.stackexchange.com/questions/18584/how-to-ask-a-smart-question-on-so/25128#25128 – 2009-10-08 20:39:20
从您选择,我们可以将答案得出结论,这确实是重复的。事实上,一个几乎完全一样的问题已经存在:http://stackoverflow.com/questions/251464/how-to-get-the-function-name-as-string-in-python – 2009-10-08 21:45:51
我不同意这是一个#251464的重复 - 看起来这个问题是反过来的。 – 2012-11-10 19:09:08