How to declare a global variable that is visible to imported module functions

Any variable globaldeclared inside the imported .py module remains global "inside" of its own module and is global "inside" the script or program where this module was imported (using Python import). Now, what if, after importing a module, Xdeclare a global variable Y. And I want to make the variable Y "visible" or globalfor functions defined in the imported module X(so I do not need to pass Yin the functions of the imported module as an argument)?

to import "myModule.py":

def printX():
    print variableX

def printY():
    global variableY
    variableY='y'
    print variableY

Main program

import myModule as myModule
myModule.printY()
print myModule.variableY

global variableX
variableX='X'
print variableX

myModule.printX() # this will result to NameError

myModule.printX() result:

NameError: global name 'variableX' is not defined
+4
1

printX variableX myModule. :

import myModule as myModule
myModule.printY()
print myModule.variableY

myModule.variableX = 10
print myModule.variableX

myModule.printX()
+3

All Articles