在文件之间使用全局variables?

我对全局variables如何工作有点困惑。 我有一个大项目,大约有50个文件,我需要为所有这些文件定义全局variables。

我所做的是在我的项目main.py文件中定义它们,如下所示:

 # ../myproject/main.py # Define global myList global myList myList = [] # Imports import subfile # Do something subfile.stuff() print(myList[0]) 

我试图在myList中使用subfile.py ,如下所示

 # ../myproject/subfile.py # Save "hey" into myList def stuff(): globals()["myList"].append("hey") 

我尝试了另一种方式,但也没有奏效

 # ../myproject/main.py # Import globfile import globfile # Save myList into globfile globfile.myList = [] # Import subfile import subfile # Do something subfile.stuff() print(globfile.myList[0]) 

subfile.py里面我有这个:

 # ../myproject/subfile.py # Import globfile import globfile # Save "hey" into myList def stuff(): globfile.myList.append("hey") 

但是,再次,它没有工作。 我应该如何执行这个? 我明白,它不能这样工作,当两个文件之间并不真正了解(好的子文件不知道主),但我想不出如何做到这一点,而不使用io写作或腌制,这我不想这样做。

问题是你从main.py中定义了myList,但是subfile.py需要使用它。 下面是一个干净的方法来解决这个问题:将所有全局variables移动到一个文件,我称这个文件settings.py 。 这个文件负责定义全局variables并初始化它们:

 # settings.py def init(): global myList myList = [] 

接下来,你的子文件可以导入全局variables:

 # subfile.py import settings def stuff(): settings.myList.append('hey') 

请注意,子文件不会调用init() – 该任务属于main.py:

 # main.py import settings import subfile settings.init() # Call only once subfile.stuff() # Do stuff with global var print settings.myList[0] # Check the result 

通过这种方式,您可以实现您的目标,同时避免多次初始化全局variables。

您可以将Python全局variables视为“模块”variables – 因此,它们比C中的传统“全局variables”更有用。

全局variables实际上是在模块的__dict__定义的,并且可以作为模块属性从该模块外部访问。

所以,在你的例子中:

 # ../myproject/main.py # Define global myList # global myList - there is no "global" declaration at module level. Just inside # function and methods myList = [] # Imports import subfile # Do something subfile.stuff() print(myList[0]) 

和:

 # ../myproject/subfile.py # Save "hey" into myList def stuff(): # You have to make the module main available for the # code here. # Placing the import inside the function body will # usually avoid import cycles - # unless you happen to call this function from # either main or subfile's body (ie not from inside a function or method) import main main.mylist.append("hey") 

使用from your_file import *应该修复你的问题。 它定义了一切,以便它是全局可用的(除了导入中的局部variables)。

例如:

 ##test.py: from pytest import * print hello_world 

和:

 ##pytest.py hello_world="hello world!" 

Hai Vu答案很棒,只是一个评论:

如果您在其他模块中使用全局,并且想要dynamic设置全局,请在设置全局variables后注意导入其他模块,例如:

 # settings.py def init(arg): global myList myList = [] mylist.append(arg) # subfile.py import settings def print(): settings.myList[0] # main.py import settings settings.init("1st") # global init before used in other imported modules # Or else they will be undefined import subfile subfile.print() # global usage 

请参阅有关跨模块共享全局variables的 Python文档:

在单个程序中跨模块共享信息的规范方法是创build一个特殊模块(通常称为config或cfg)。

config.py:

 x = 0 

在应用程序的所有模块中导入configuration模块; 该模块将成为全球名称。

main.py:

 import config print (config.x) 

要么

 from config import x print (x) 

一般来说, 不要使用模块名称导入* 。 这样做会混淆import商的名称空间,使得linters更难检测未定义的名称。

你的第二次尝试将完美地工作,而且实际上是一个非常好的方法来处理你想要全局可用的variables名称。 但是在最后一行你有一个名字错误。 这是应该如何:

 # ../myproject/main.py # Import globfile import globfile # Save myList into globfile globfile.myList = [] # Import subfile import subfile # Do something subfile.stuff() print(globfile.myList[0]) 

看最后一行? myList是globfile的一个属性,而不是子文件。 这将工作,你想要的。

麦克风