Search notes:

Python: using the global statement in modules

The following example tries to demonstrate the usage of the global statement in a module.

The module (tq84_mod.py)

The module has one function, func(), which assigns a value (assigned in module's func()) to a global variable. This variable is global within the module, not within the entire execution.
def func():
    global var
    var = "assigned in module's func()"
Github repository about-python, path: /statements/global/module/tq84_mod.py

The script

The script imports the module, assigns a value to var calls func() in the module and then prints the module's and the script's value of var.
def func():
    global var
    var = "assigned in module's func()"
Github repository about-python, path: /statements/global/module/tq84_mod.py
Because the script's (global) var variable is another variable than the module's global var variable, the script, when executed, prints:
var          = assigned in script
tq84_mod.var = assigned in module's func()

Index