I am a bit confused about how global variables work. I have a large project with about 50 files, and I need to define global variables for all these files.
What I did was define them in my main.py projects, as shown below:
# ../myproject/main.py
I am trying to use myList in subfile.py as below
# ../myproject/subfile.py # Save "hey" into myList def stuff(): globals()["myList"].append("hey")
In another way, I tried, but didnโt work either
# ../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])
And inside subfile.py I had this:
# ../myproject/subfile.py # Import globfile import globfile # Save "hey" into myList def stuff(): globfile.myList.append("hey")
But then again, this did not work. How do I implement this? I understand that it cannot work when these two files really do not know each other (the necessary subfile does not know the basic information), but I canโt figure out how to do this without using an io letter or pickle, which I donโt want this do.
python share globals
user1632861 Oct 23 '12 at 15:54 2012-10-23 15:54
source share