从调用者的全局变量中获取变量.什么是框架对象? [英] Getting a variable from the caller's globals. What is a frame object?

查看:101
本文介绍了从调用者的全局变量中获取变量.什么是框架对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查模块的文档说:

当以下函数返回框架记录"时,每个记录都是 命名为元组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])

推荐答案

来自因此可以在my_util.py中获得一些全局变量:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆