How, in Python, do we create global variable that behave like in C/C++?
I mean, I'm in a team that shares most part of our code base with others teams. Each team has it own main file and function. And, some (lots) parts of the code have to behave in a slightly different way depending on the team that runs it.
How can I set a variable in the different mains, and then get it's value in every other files?
Here is what I tried, but in the end, team
is always None
in every files and functions/methods.
I also tried all combination of adding global
before accessing the variable team
, the result is still the same.
# common.py
team = None
# main_a.py
from common import team
def main():
...
team = "A"
...
func()
# main_b.py
from common import team
def main():
...
team = "B"
...
func()
# random_file.py
from common import team
def func():
...
var = 3 if team == "A" else 5
...
I know there is a solution like 1 and 2, but it does not fit my needs, as the "global" variable is modified in the child function, and then read in the parent one, which is the opposite of what I'm trying to achieve.