Questions tagged «globals»

6
在文件之间使用全局变量?
我对全局变量的工作方式感到困惑。我有一个大型项目,大约有50个文件,我需要为所有这些文件定义全局变量。 我所做的就是在我的项目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 = [] # …
207 python  share  globals 
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.