Python从导入的文件中提取变量 [英] Python extract variables from an imported file

查看:149
本文介绍了Python从导入的文件中提取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个文件.一种是定义变量,另两种包含必需的模块.

I've 3 files. One is for defining the variables and other two contain the required modules.

variable.py

my_var = ""

test.py

import variable

def trial():
    variable.my_var = "Hello"

main.py (不起作用)

from variable import *
from test import trial

if __name__ == "__main__":
    trial()
    print my_var

当我运行main.py时,它什么也没给出.但是,如果我这样更改main.py

And when I run main.py, it gives nothing. However, if I change main.py like this,

main.py (有效)

import variable
from test import trial

if __name__ == "__main__":
    trial()
    print variable.my_var

它给了我预期的输出,即Hello.

And it gives me the expected output i.e. Hello.

以我为例,该variable.py包含更多变量,我不想在访问它们时使用variable.<variable name>.在修改它们时,使用variable.<variable name>不会有问题,因为我只会根据用户的输入进行一次修改,然后在其余时间多次跨多个文件访问它们.

That variable.py, in my case, contains more variables and I don't want to use variable.<variable name> while accessing them. While modifying them, using variable.<variable name> is not a problem as I'm gonna modify only once depending on the user's input and then access them across multiple files multiple times rest of the time.

现在我的问题是,有没有办法在test.py修改后将main.py中的所有变量提取出来并使用不带前缀variable.的变量?

Now my question is, is there a way to extract all the variables in main.py after they are modified by test.py and use them without the prefix variable.?

推荐答案

不可能实现. from MODULE import *将所有变量名称和值导入您的名称空间,但不导入变量本身.变量是在您的本地名称空间中分配的,因此更改其值不会反映在它们的来源上.但是,由于您导入的值基本上是对实例的引用,因此会反映出对可变对象的更改.

What you want to achieve is not possible. from MODULE import * imports all the variables names and the values into your namespace, but not the variables themselves. The variables are allocated in your local namespace and changing their value is therefore not reflected onto their origin. However, changes on mutable objects are reflected because the values you import are basically the references to instances.

这是为什么不建议使用上述结构的不合格进口的许多原因之一.相反,您应该质疑程序结构和所做的设计决策.从长远来看,使用大量的模块级变量会变得很麻烦并且非常不便.

That is one of the many reasons why unqualified imports using the above construct are not recommended. Instead, you should question your program structure and the design decisions you took. Working with a huge amount of module-level variables can become cumbersome and very inconvenient in the long term.

考虑对您的应用程序使用更结构化的方法,例如通过使用类.

Think about using a more structured approach to your application, for example by using classes.

再次,我想指出并赞扬此线程,从本质上讲,它解决了同一问题.

Again I would like to point out and give credits to this thread which, in essence, addresses the same problem.

编辑,出于完整性考虑.可以,但是不建议使用此方法.每次对非引用变量进行修改时,您都必须重新导入该模块:

EDIT For the sake of completeness. It is possible, but this approach is not recommended. You would have to re-import the module every time you apply modifications to its non-referential variables:

from variable import *  # optional
from test import trial

if __name__ == "__main__":
    trial()
    from variable import *  # simply re-import the variables...
    print my_var

这篇关于Python从导入的文件中提取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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