In Python, an access variable defined mostly from another module

I am writing a Python program in which the main script initializes several variables with shared data from the command line, config file, etc. I want the rest of the program to be able to access them as global:

def abc() :
    global xyz
    _someOptionValue = xyz['someOptionName']
    ...

      

This does not work. I think this is because the global is defined in main.py

(for example) and the function that refers to it is defined in abc.py

(for example). Hence the global function namespace is different from the main script namespace.

I think I can manage to get the global variable by assigning a name to it with the name of the main script namespace, but what is that namespace name?

Context: I am passing these variables as parameters and I know that many programmers find this to be a cleaner solution. In this case, I'm not because the variables are set once and then read throughout the program. In my opinion, this is the strongest case (and the only justified case) for using globals. If you think that I shouldn't do this, I respect your opinion, but I do not share it; please respect me!

+3


source to share


1 answer


Create a module globals

that contains all the general information and each module (including the main one) will import this module.



+2


source







All Articles