Reputation: 635
I have several python functions that I am trying to create into a package. Each function exists in its own python file, and uses global variables to return some objects back to the global environment , some of which get used by the other python functions.
When these functions are standalone functions that have been defined in the python console, they work just fine, but when I put them all together into a python package, the global variables are not being returned as a global variable any longer.
Why do functions that are defined with a package file not return global variables / how can I bypass this?
A very simple example:
python_function1.py
def function1(x):
global new_table
new_table = x
python_function2.py
def function2(new_table):
global new_table2
new_table2 = new_table
Upvotes: 0
Views: 1625
Reputation: 564
As per documentation states:
The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Just import the config module in all modules of your application; the module then becomes available as a global name. Because there is only one instance of each module, any changes made to the module object get reflected everywhere.
You can check this documentation for example code:
https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules
Upvotes: 5