一个函数可以访问自己的名字吗? [英] Can a function access its own name?
问题描述
请看下面的代码:
#mystringfunctions.py
def cap(s):
print s
print"此功能的名称是 +" ???"
cap(" hello")
运行上面的代码会给出以下输出
hello
此函数的名称是???
我希望输出为
你好
这个函数的名称是cap
这可能吗?
bo*******@yahoo.com 写道:看看代码如下:
#mystringfunctions.py
def cap(s):
print s
print"这个函数的名称是 +" ???"
cap(" hello")
运行上面的代码会得到以下输出
>>>你好
这个函数的名字是??? >>>
我希望输出为
>>>你好
这个函数的名字是cap>>>
这可能吗?
是的,使用模数检查来访问当前的堆栈框架:
def handle_stackframe_without_leak():
frame = inspect.currentframe()
尝试:
打印inspect.getframeinfo(框架)[2]
终于:
del frame
Diez
bo *** ****@yahoo.com 写道:看下面的代码:
#mystringfunctions.py
def cap (s):
print s
print"此功能的名称是 +" ???"
cap(" hello")
运行上面的代码会得到以下输出
>>>你好
这个函数的名字是??? >>>
我希望输出为
>>>你好
这个函数的名字是cap>>>
这可能吗?
是的,使用模数检查来访问当前的堆栈框架:
def handle_stackframe_without_leak():
frame = inspect.currentframe()
尝试:
打印inspect.getframeinfo(框架)[2]
终于:
del frame
Diez
bo *** ****@yahoo.com 写道:
[略微编辑]def cap():
打印这个名字功能是 +" ???"
cap()
sys._getframe()会帮助你:
import sys
sys._getframe()
< frame object at 0x00B496D0> def f():
.... global x
.... x = sys._getframe()
.... f()
x
< 0x00B15250处的帧对象> dir(x)
[...,''f_builtins'',''f_code'',''f_exc_traceback'',''f_exc_type'',...] dir(x.f_code)
[...''co_name'',''co_names'',''co_nlocals'',''co_stacksize'',''co_varnames''] x.f_code.co_name
''f''
所以你的功能可能是:
import sys
def cap():
.... print''函数名是'',sys._getframe()。f_code.co_name
.... cap()
函数名是上限
-Peter
Look at the code below:
# mystringfunctions.py
def cap(s):
print s
print "the name of this function is " + "???"
cap ("hello")
Running the code above gives the following output
hello
the name of this function is ???
I would like the output to be
hello
the name of this function is cap
Is that possible ?
bo*******@yahoo.com wrote:Look at the code below:
# mystringfunctions.py
def cap(s):
print s
print "the name of this function is " + "???"
cap ("hello")
Running the code above gives the following output>>> hello
the name of this function is ??? >>>
I would like the output to be
>>> hello
the name of this function is cap >>>
Is that possible ?
Yes, use the moduloe inspect to access the current stack frame:
def handle_stackframe_without_leak():
frame = inspect.currentframe()
try:
print inspect.getframeinfo(frame)[2]
finally:
del frame
Diez
bo*******@yahoo.com wrote:Look at the code below:
# mystringfunctions.py
def cap(s):
print s
print "the name of this function is " + "???"
cap ("hello")
Running the code above gives the following output>>> hello
the name of this function is ??? >>>
I would like the output to be
>>> hello
the name of this function is cap >>>
Is that possible ?
Yes, use the moduloe inspect to access the current stack frame:
def handle_stackframe_without_leak():
frame = inspect.currentframe()
try:
print inspect.getframeinfo(frame)[2]
finally:
del frame
Diez
bo*******@yahoo.com wrote:
[edited slightly]def cap():
print "the name of this function is " + "???"
cap ()
sys._getframe() would help you here:
import sys
sys._getframe() <frame object at 0x00B496D0> def f(): .... global x
.... x = sys._getframe()
.... f()
x <frame object at 0x00B15250> dir(x) [..., ''f_builtins'', ''f_code'', ''f_exc_traceback'', ''f_exc_type'', ...] dir(x.f_code) [...''co_name'', ''co_names'', ''co_nlocals'', ''co_stacksize'', ''co_varnames''] x.f_code.co_name ''f''
So your function could be:
import sys
def cap(): .... print ''function name is'', sys._getframe().f_code.co_name
.... cap()
function name is cap
-Peter
这篇关于一个函数可以访问自己的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!