我inspect.getouterframes(inspect.currentframe())[1]
用来在 Python 2.7 中获取调用者的框架。我想检查调用是否来自 return 语句。这是因为我正在与使用 python 作为一种工具包语言的系统进行交互,并带有一些认为这很有用的约定。
是否有结构化的方法来检查调用者是否从 return 语句“执行当前帧” ?
我试图检查源代码,inspect.getsourcelines(frame)
但如果调用者代码混乱,它看起来并不是一个好主意:
if foo(3, 'ugly # evil\' :"string"', """hey"""): return called(1, # arg1
my_favorite_constant, 'very long stuff', 'bad conventions',3) # comment
called(*stuff) # hey, not return -ing anything here.
所以做结构检查比字符串解析更好。我正在浏览检查模块文档,但不知道该怎么做。