zs2*_*020 166 python introspection
Python:如何在被调用的方法中获取调用者的方法名?
假设我有两种方法:
def method1(self):
...
a = A.method2()
def method2(self):
...
Run Code Online (Sandbox Code Playgroud)
如果我不想对method1进行任何更改,如何在method2中获取调用者的名称(在此示例中,名称为method1)?
Ale*_*lli 209
inspect.getframeinfo和其他相关函数inspect可以帮助:
>>> import inspect
>>> def f1(): f2()
...
>>> def f2():
... curframe = inspect.currentframe()
... calframe = inspect.getouterframes(curframe, 2)
... print('caller name:', calframe[1][3])
...
>>> f1()
caller name: f1
Run Code Online (Sandbox Code Playgroud)
这种内省旨在帮助调试和开发; 不建议将其用于生产功能目的.
Tod*_*wen 83
更短的版本:
import inspect
def f1(): f2()
def f2():
print 'caller name:', inspect.stack()[1][3]
f1()
Run Code Online (Sandbox Code Playgroud)
(感谢@Alex和Stefaan Lippen)
Aug*_*wan 53
这似乎工作得很好:
import sys
print sys._getframe().f_back.f_code.co_name
Run Code Online (Sandbox Code Playgroud)
ana*_*nik 25
我想出了一个稍微长一点的版本,试图建立一个完整的方法名称,包括模块和类.
https://gist.github.com/2151727(rev 9cccbf)
# Public Domain, i.e. feel free to copy/paste
# Considered a hack in Python 2
import inspect
def caller_name(skip=2):
"""Get a name of a caller in the format module.class.method
`skip` specifies how many levels of stack to skip while getting caller
name. skip=1 means "who calls me", skip=2 "who calls my caller" etc.
An empty string is returned if skipped levels exceed stack height
"""
stack = inspect.stack()
start = 0 + skip
if len(stack) < start + 1:
return ''
parentframe = stack[start][0]
name = []
module = inspect.getmodule(parentframe)
# `modname` can be None when frame is executed directly in console
# TODO(techtonik): consider using __main__
if module:
name.append(module.__name__)
# detect classname
if 'self' in parentframe.f_locals:
# I don't know any way to detect call from the object method
# XXX: there seems to be no way to detect static method call - it will
# be just a function call
name.append(parentframe.f_locals['self'].__class__.__name__)
codename = parentframe.f_code.co_name
if codename != '<module>': # top level usually
name.append( codename ) # function or a method
## Avoid circular refs and frame leaks
# https://docs.python.org/2.7/library/inspect.html#the-interpreter-stack
del parentframe, stack
return ".".join(name)
Run Code Online (Sandbox Code Playgroud)
mig*_*k35 10
上面的东西融合了一点点.但这是我对它的抨击.
def print_caller_name(stack_size=3):
def wrapper(fn):
def inner(*args, **kwargs):
import inspect
stack = inspect.stack()
modules = [(index, inspect.getmodule(stack[index][0]))
for index in reversed(range(1, stack_size))]
module_name_lengths = [len(module.__name__)
for _, module in modules]
s = '{index:>5} : {module:^%i} : {name}' % (max(module_name_lengths) + 4)
callers = ['',
s.format(index='level', module='module', name='name'),
'-' * 50]
for index, module in modules:
callers.append(s.format(index=index,
module=module.__name__,
name=stack[index][3]))
callers.append(s.format(index=0,
module=fn.__module__,
name=fn.__name__))
callers.append('')
print('\n'.join(callers))
fn(*args, **kwargs)
return inner
return wrapper
Run Code Online (Sandbox Code Playgroud)
使用:
@print_caller_name(4)
def foo():
return 'foobar'
def bar():
return foo()
def baz():
return bar()
def fizz():
return baz()
fizz()
Run Code Online (Sandbox Code Playgroud)
输出是
level : module : name
--------------------------------------------------
3 : None : fizz
2 : None : baz
1 : None : bar
0 : __main__ : foo
Run Code Online (Sandbox Code Playgroud)
我会用inspect.currentframe().f_back.f_code.co_name。先前的任何答案都未涵盖其使用,这些答案主要是以下三种类型之一:
inspect.stack但是众所周知它太慢了。sys._getframe鉴于其下划线,某些用法是内部私有函数,因此不建议使用。inspect.getouterframes(inspect.currentframe(), 2)[1][3]但目前尚不清楚[1][3]正在访问什么。import inspect
import types
from typing import cast
def caller_name() -> str:
"""Return the calling function's name."""
# Ref: https://stackoverflow.com/a/57712700/
return cast(types.FrameType, inspect.currentframe()).f_back.f_code.co_name
if __name__ == '__main__':
def _test_caller_name() -> None:
assert caller_name() == '_test_caller_name'
_test_caller_name()
Run Code Online (Sandbox Code Playgroud)
致谢:1313e事先发表评论以寻求答案。