从调用者的全局变量中获取变量.什么是框架对象? [英] Getting a variable from the caller's globals. What is a frame object?
问题描述
检查模块的文档说:
当以下函数返回框架记录"时,每个记录都是 命名为元组
FrameInfo(frame, filename, lineno, function, code_context, index)
.元组包含框架对象,文件名,行 当前行号,函数名称,行列表 源代码中的上下文,以及其中的当前行的索引 该列表.
When the following functions return "frame records," each record is a named tuple
FrameInfo(frame, filename, lineno, function, code_context, index)
. The tuple contains the frame object, the filename, the line number of the current line, the function name, a list of lines of context from the source code, and the index of the current line within that list.
什么是框架对象" ?我希望使用此框架对象从调用方的globals()
中获取变量的值:
What is actually a "frame object"? I was hoping to use this frame object to get a variable's value from the caller's globals()
:
import my_util
a=3
my_util.get_var('a')
和 my_util.py
import inspect
def get_var(name):
print(inspect.stack()[1][0])
推荐答案
therefore to get some globals in your my_util.py:
import inspect
def get_var(name):
print(inspect.stack()[1][0].f_globals[name])
这篇关于从调用者的全局变量中获取变量.什么是框架对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!