I'm working on creating a python program and I just finished the login/signup functionality. It would be ideal for me to have a user
variable (that contains user info) that I could reference throughout the application. How would I do that? Is the answer just constantly passing around a variable from file to file (that seems a little tedious) or is there another solution.
In the same vein, I also want to do some state management (was looking at maybe using a library like pytransitions) that would keep track of different user states. Is there a way to have the state be globally accessible throughout the application? That would make it really easily to access and change the state as needed.
I know there are global variables in python but it seems like those are just 'global' within a file. So if I have file1.py
with a global variable called someVar
, that won't, per my understanding, be accessible within another file file2.py
CodePudding user response:
Move all globals to a file, I call this file settings.py. This file is responsible for defining globals and initializing them:
# settings.py
def init():
global myList
myList = []
Next, your subfile can import globals:
# subfile.py
import settings
credits to globalvariables between files