def applejuice(q):
print THE FUNCTION NAME!
It should result in "applejuice" as a string.
它应该会导致“苹果汁”成为一个字符串。
7 个解决方案
#1
19
This also works:
这同样适用:
import sys
def applejuice(q):
func_name = sys._getframe().f_code.co_name
print func_name
#2
9
def applejuice(**args):
print "Running the function 'applejuice'"
pass
or use:
或者使用:
myfunc.__name__
>>> print applejuice.__name__
'applejuice'
Also, see how-to-get-the-function-name-as-string-in-python
同时,看到how-to-get-the-function-name-as-string-in-python
#3
7
import traceback
def applejuice(q):
stack = traceback.extract_stack()
(filename, line, procname, text) = stack[-1]
print procname
I assume this is used for debugging, so you might want to look into the other procedures offered by the traceback
module. They'll let you print the entire call stack, exception traces, etc.
我假设这是用于调试的,因此您可能想了解traceback模块提供的其他过程。它们将让您打印整个调用堆栈、异常跟踪等等。
#4
3
Another way
另一种方式
import inspect
def applejuice(q):
print inspect.getframeinfo(inspect.currentframe())[2]
#5
2
You need to explain what your problem is. Because the answer to your question is:
你需要解释你的问题是什么。因为你的问题的答案是:
print "applejuice"
#6
1
This site gave me a decent explanation of how sys._getframe.f_code.co_name works that returns the function name.
这个站点给了我一个关于sys._getframe.f_code的很好的解释。co_name工作,返回函数名。
http://code.activestate.com/recipes/66062-determining-current-function-name/
http://code.activestate.com/recipes/66062-determining-current-function-name/
#7
0
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)
#1
19
This also works:
这同样适用:
import sys
def applejuice(q):
func_name = sys._getframe().f_code.co_name
print func_name
#2
9
def applejuice(**args):
print "Running the function 'applejuice'"
pass
or use:
或者使用:
myfunc.__name__
>>> print applejuice.__name__
'applejuice'
Also, see how-to-get-the-function-name-as-string-in-python
同时,看到how-to-get-the-function-name-as-string-in-python
#3
7
import traceback
def applejuice(q):
stack = traceback.extract_stack()
(filename, line, procname, text) = stack[-1]
print procname
I assume this is used for debugging, so you might want to look into the other procedures offered by the traceback
module. They'll let you print the entire call stack, exception traces, etc.
我假设这是用于调试的,因此您可能想了解traceback模块提供的其他过程。它们将让您打印整个调用堆栈、异常跟踪等等。
#4
3
Another way
另一种方式
import inspect
def applejuice(q):
print inspect.getframeinfo(inspect.currentframe())[2]
#5
2
You need to explain what your problem is. Because the answer to your question is:
你需要解释你的问题是什么。因为你的问题的答案是:
print "applejuice"
#6
1
This site gave me a decent explanation of how sys._getframe.f_code.co_name works that returns the function name.
这个站点给了我一个关于sys._getframe.f_code的很好的解释。co_name工作,返回函数名。
http://code.activestate.com/recipes/66062-determining-current-function-name/
http://code.activestate.com/recipes/66062-determining-current-function-name/
#7
0
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)