相当于 Python 中的 __func__(来自 C) [英] Equivalent of __func__ (from C) in Python
本文介绍了相当于 Python 中的 __func__(来自 C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为 python 创建某种调试输出,并想将函数名传递给另一个函数进行输出.
I want to create some kind of debugging output for python and want to pass the functionname to another function for output.
我发现这样做的唯一合理方法是:
The only reasonable way I found to do so was:
def foobar():
print 'hello world'
print foobar.__name__
是否有对当前函数做同样的事情来改进该行的复制和粘贴的东西?
is there something that does the same thing for the current function to improve copy and pasting of that line?
像 C 等价的 printf("%s", __func__)
.
推荐答案
sys._getframe 可以做到:
import sys
def foobar():
print sys._getframe().f_code.co_name
这篇关于相当于 Python 中的 __func__(来自 C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文