来自不同文件Python的全局变量

Ano*_*non 29 python

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

file1.py

from file2 import *
foo = "bar";
test = SomeClass();
Run Code Online (Sandbox Code Playgroud)

file2.py

class SomeClass :
    def __init__ (self):
        global foo;
        print foo;
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*lli 52

导入file2file1.py使得全球(即,模块级)的约束名字file2提供给下面的代码file1-唯一的此类名称是SomeClass.它并没有做反向:在定义的名称file1并不提供给代码file2的时候file1进口file2.即使你以正确的方式导入(import file2正如@nate正确推荐的那样)而不是你正在做的那种可怕的,可怕的方式(如果太阳下的每个人都忘记了构造的存在,那么from ... import *生活就会如此)所以多给大家更好).

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

所以,我不是要向你展示难以置信的脆弱,通常难以维护的黑客来实现(某些外表)Python中的循环依赖,而是更多地讨论你可以避免这种糟糕结构的许多优秀方式.

例如,你可以把需要都可获得的模块中全局名字第三模块(例如file3.py,继续你的命名连胜;-)并导入第三模块到每个其他两个(import file3file1file2,然后用file3.foo等,即,合格的名称,用于访问或设定从其它模块,的任一个或两个那些全局名称的目的 barenames).

当然,如果你澄清(编辑您的Q)更准确和更具体的帮助,可以提供为什么不管是什么让你觉得你需要一个循环依赖,你是:你认为你需要一个循环依赖(只是一个简单的预测错误;-).

  • `file1.py`可以编辑`file3.py`中的全局变量,`file2.py`会自动重新加载新的全局变量吗? (3认同)

Dav*_*d Z 14

当你写作

from file2 import *
Run Code Online (Sandbox Code Playgroud)

它实际上将定义的名称复制file2到命名空间中file1.所以如果你file1通过写作重新分配这些名字

foo = "bar"
Run Code Online (Sandbox Code Playgroud)

例如,它只会进行更改file1,而不是file2.请注意,如果你要改变一个属性foo,通过做说

foo.blah = "bar"
Run Code Online (Sandbox Code Playgroud)

然后,该更改将反映在file2,因为您正在修改名称引用的现有对象foo,而不是用新对象替换它.

您可以通过以下方式获得所需的效果file1.py:

import file2
file2.foo = "bar"
test = SomeClass()
Run Code Online (Sandbox Code Playgroud)

(请注意,您应该删除from foo import *)虽然我建议您仔细考虑是否真的需要这样做.从另一个模块中更改一个模块的变量并不是很常见.


nat*_*e c 9

from file2 import *正在制作副本.你想这样做:

import file2
print file2.foo
print file2.SomeClass()
Run Code Online (Sandbox Code Playgroud)


msw*_*msw 6

global在 Python 中有点用词不当,module_namespace会更具描述性。

最好避免使用fooisfile1.foo和 global 语句的完全限定名称,因为通常有更好的方法来完成您想做的事情。(我无法从您的玩具示例中看出您想做什么。)


Wah*_*ram 5

搜索后,我得到了这条线索:https://instructobit.com/tutorial/108/How-to-share-global-variables- Between-files-in-Python

关键是:打开该功能,如果功能激活,则调用设置为全局的变量。

然后再次从该文件导入变量。

我给你举个困难的例子,这样你就可以理解:

文件 chromy.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def opennormal():
    global driver
    options = Options()
    driver = webdriver.Chrome(chrome_options=options)

def gotourl(str):
    url = str
    driver.get(url)
Run Code Online (Sandbox Code Playgroud)

文件测试器.py

from chromy import * #this command call all function in chromy.py, but the 'driver' variable in opennormal function is not exists yet. run: dir() to check what you call.

opennormal() #this command activate the driver variable to global, but remember, at the first import you not import it

#then do this, this is the key to solve:
from chromy import driver #run dir() to check what you call and compare with the first dir() result.

#because you already re-import the global that you need, you can use it now

url = 'https://www.google.com'
gotourl(url)
Run Code Online (Sandbox Code Playgroud)

这就是调用在函数中设置的全局变量的方式。干杯别忘了给予信任