在Python中,如果不使用traceback模块,是否有一种方法可以从函数内部确定函数的名称?
假设我有一个模块foo和一个功能栏。当执行foo.bar()时,是否有一种方法让bar知道bar的名字?或者更好的是,foo。酒吧的名字吗?
#foo.py
def bar():
print "my name is", __myname__ # <== how do I calculate this at runtime?
在Python中,如果不使用traceback模块,是否有一种方法可以从函数内部确定函数的名称?
假设我有一个模块foo和一个功能栏。当执行foo.bar()时,是否有一种方法让bar知道bar的名字?或者更好的是,foo。酒吧的名字吗?
#foo.py
def bar():
print "my name is", __myname__ # <== how do I calculate this at runtime?
当前回答
@jeff-laughlin的回答很漂亮。我对它进行了轻微的修改,以达到我认为的目的:跟踪函数的执行,并捕获参数列表以及关键字参数。谢谢你@jeff-laughlin!
from functools import wraps
import time
def named(func):
@wraps(func)
def _(*args, **kwargs):
print(f"From wrapper function: Executing function named: {func.__name__}, with arguments: {args}, and keyword arguments: {kwargs}.")
print(f"From wrapper function: {func}")
start_time = time.time()
return_value = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"From wrapper function: Execution of {func.__name__} took {elapsed_time} seconds.")
return return_value
return _
@named
def thanks(message, concepts, username='@jeff-laughlin'):
print(f"From inner function: {message} {username} for teaching me about the {concepts} concepts of closures and decorators!")
thanks('Thank you', 'two', username='@jeff-laughlin')
print('-'*80)
thanks('Thank you', 'two', username='stackoverflow')
print(thanks)
From wrapper function: Executing function named: thanks, with arguments: ('Thank you', 'two'), and keyword arguments: {'username': '@jeff-laughlin'}. From wrapper function: <function thanks at 0x7f13e6ceaa60> From inner function: Thank you @jeff-laughlin for teaching me about the two concepts of closures and decorators! From wrapper function: Execution of thanks took 2.193450927734375e-05 seconds. -------------------------------------------------------------------------------- From wrapper function: Executing function named: thanks, with arguments: ('Thank you', 'two'), and keyword arguments: {'username': 'stackoverflow'}. From wrapper function: <function thanks at 0x7f13e6ceaa60> From inner function: Thank you stackoverflow for teaching me about the two concepts of closures and decorators! From wrapper function: Execution of thanks took 7.152557373046875e-06 seconds. <function thanks at 0x7f13e6ceaca0>
最让我惊讶的是,有一种方法可以在运行时拦截函数,检查它们,并基于此采取一些操作。另一件令人惊讶的事情是内部函数的内存地址两次都是相同的。有人知道这是为什么吗?在我能够理解这个装饰器/闭包魔法之前,我还有一段路要走。
其他回答
import inspect
def method_name():
return inspect.stack()[1][3]
def method_name_caller():
return inspect.stack()[2][3]
def asdf():
print(method_name_caller())
print(method_name())
def asdf2():
print(method_name_caller())
print(method_name())
asdf()
你可以使用@Andreas Jung显示的方法获得它定义时的名称,但这可能不是调用函数时的名称:
import inspect
def Foo():
print inspect.stack()[0][3]
Foo2 = Foo
>>> Foo()
Foo
>>> Foo2()
Foo
这种区别对你来说是否重要,我不能说。
实现您自己的装饰器
# mydecorators.py
def resolve_function(func):
#in case annotated func is an staticmethod
if isinstance(func,staticmethod):
return func.__func__
return func
def print_my_name(func):
def function_caller(*args,**kwargs):
_func = resolve_function(func)
print("my name is: %s" %_func.__name__)
return _func(*args,**kwargs)
return function_caller
然后使用它
# foo.py
from mydecorators import *
@print_my_name
def bar():
#do something else
#in terminal: my name is: bar
我找到了一个包装器,它将写入函数名
from functools import wraps
def tmp_wrap(func):
@wraps(func)
def tmp(*args, **kwargs):
print func.__name__
return func(*args, **kwargs)
return tmp
@tmp_wrap
def my_funky_name():
print "STUB"
my_funky_name()
这将打印
my_funky_name 存根
我不知道为什么人们会把它弄得这么复杂:
import sys
print("%s/%s" %(sys._getframe().f_code.co_filename, sys._getframe().f_code.co_name))