【发布时间】:2016-02-05 14:49:44
【问题描述】:
在显示调用函数的参数和值的实用函数中,我需要知道从另一个模块导入的可能别名函数的原始名称。这可能是针对导入别名时的简单情况吗?
这是一个简化的用例,我首先展示来自utilities.py 模块的一些代码:
import inspect
DEBUG_FLAG = True
def _log_args(*args):
"""Uses reflection to returning passing argument code with values."""
prev_frame = inspect.currentframe().f_back
func_name = prev_frame.f_code.co_name
code_context = inspect.getframeinfo(prev_frame.f_back).code_context[0].strip()
# Do some magic, which does work _unless_ func_name is aliased :-)
print('code context: {}'.format(code_context))
print('func_name : {}'.format(func_name))
return ', '.join(str(arg) for arg in args)
def format_args(*args):
"""Returns string with name of arguments with values."""
return _log_args(args)
def debug_print(*args):
"""Prints name of arguments with values."""
if DEBUG_FLAG:
print _log_args(args)
这里有一些代码首先通过原始名称访问这些函数,然后通过别名:
from utilities import debug_print, format_args, debug_print as debug, format_args as fargs
def main():
a, b = "text", (12, 13)
print "== Unaliased =="
test_text = format_args(a, b)
print test_text # Returns
debug_print(a, b)
print "\n== Aliased =="
test_text = fargs(a, b)
print test_text
debug(a, b)
if __name__ == '__main__':
main()
这个输出是:
== Unaliased ==
code context: test_text = format_args(a, b)
func_name : format_args
('text', (12, 13))
code context: debug_print(a, b)
func_name : debug_print
('text', (12, 13))
== Aliased ==
code context: test_text = fargs(a, b)
func_name : format_args
('text', (12, 13))
code context: debug(a, b)
func_name : debug_print
('text', (12, 13))
可以看出,我找到了正确的代码上下文,并且找到了调用函数的名称,但可惜第一个报告的是别名,而后者报告的是实际名称。所以我的问题是是否可以反转操作,以便我可以知道format_args 已别名为fargs,而debug_print 已别名为debug? p>
一些相关的问题,没有解决了这种别名的逆转:
【问题讨论】:
-
简短回答:不,没有,不是没有对调用框架的源代码进行广泛的 AST 解析和分析,因此您可以猜测用于产生调用的名称。
-
@MartijnPieters,AST?那是抽象语法树吗?
-
是的,您必须加载源代码,然后分析调用是如何进行的以及可调用对象的名称。请注意,您可以创建其他不一定有名称的引用;
callables = [fargs, debug], thencallables[0]()` 使用对列表中函数对象的引用。 -
@MartijnPieters,正如开头所述,我的目标是简单的别名,所以如果有人通过使用像你的
callables这样的东西破坏了这个功能,那么他们就靠自己了! :-) -
@MartijnPieters,由于我有部分代码上下文,我可以尝试其中的单词并检查它们是否评估为我的两种方法之一吗? (在某些字典中使用查找...)
标签: python python-2.7 reflection