来自不同文件 Python 的全局变量 [英] Global Variable from a different file Python

查看:79
本文介绍了来自不同文件 Python 的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个不同的文件,有点像这样:

So I have two different files somewhat like this:

file1.py

from file2 import *
foo = "bar";
test = SomeClass();

file2.py

class SomeClass :
    def __init__ (self):
        global foo;
        print foo;

但是我似乎无法让 file2 识别来自 file1 的变量,即使它已经导入到 file1 中.如果能以某种方式实现,那将非常有帮助.

However I cannot seem to get file2 to recognize variables from file1 even though its imported into file1 already. It would be extremely helpful if this is possible in some way.

推荐答案

file1.py 中导入 file2 使全局(即模块级别)名称绑定在 file2 可用于 file1 中的以下代码——唯一的名称是 SomeClass.它不会做相反的事情:当file1导入时,file1中定义的名称不能用于file2中的代码file2.即使您以正确的方式导入(import file2,正如@nate 正确推荐的),而不是以您正在做的可怕的、可怕的方式(如果太阳底下的每个人都忘记了结构from ... import *的存在,对每个人来说生活都会so好得多).

Importing file2 in file1.py makes the global (i.e., module level) names bound in file2 available to following code in file1 -- the only such name is SomeClass. It does not do the reverse: names defined in file1 are not made available to code in file2 when file1 imports file2. This would be the case even if you imported the right way (import file2, as @nate correctly recommends) rather than in the horrible, horrible way you're doing it (if everybody under the Sun forgot the very existence of the construct from ... import *, life would be so much better for everybody).

显然,您想让 file1 中定义的全局名称可用于 file2 中的代码,反之亦然.这被称为循环依赖",是一个可怕的想法(在 Python 或其他任何地方).

Apparently you want to make global names defined in file1 available to code in file2 and vice versa. This is known as a "cyclical dependency" and is a terrible idea (in Python, or anywhere else for that matter).

因此,与其向您展示在 Python 中实现(某种表面上的)循环依赖的极其脆弱、通常无法维护的 hack,我更愿意讨论您可以避免 如此糟糕的结构.

So, rather than showing you the incredibly fragile, often unmaintainable hacks to achieve (some semblance of) a cyclical dependency in Python, I'd much rather discuss the many excellent way in which you can avoid such terrible structure.

例如,您可以将需要对两个模块都可用的全局名称放在第三个​​模块中(例如 file3.py,以继续您的连续命名;-) 并将第三个模块导入其他两个模块中的每一个(file1file2 中的 import file3,然后使用 file3.foo 等,即限定名称,用于从其他模块中的一个或两个访问或设置这些全局名称,不是裸名).

For example, you could put global names that need to be available to both modules in a third module (e.g. file3.py, to continue your naming streak;-) and import that third module into each of the other two (import file3 in both file1 and file2, and then use file3.foo etc, that is, qualified names, for the purpose of accessing or setting those global names from either or both of the other modules, not barenames).

当然,如果您明确(通过编辑 Q)为什么您认为自己需要循环依赖(只是一个简单的预测:无论是什么使您认为你需要一个循环依赖,你错了;-)

Of course, more and more specific help could be offered if you clarified (by editing your Q) exactly why you think you need a cyclical dependency (just one easy prediction: no matter what makes you think you need a cyclical dependency, you're wrong;-).

这篇关于来自不同文件 Python 的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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