0

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
Nate Thompson
  • 625
  • 1
  • 7
  • 22
  • 4
    Perhaps you could update your question with example code, showing a simple case where multiple functions use global variables. – quamrana Feb 19 '18 at 16:12
  • You might check out this question. [Why Globals are evil](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil) – Zack Tarr Feb 19 '18 at 16:13
  • Are you under the impression that when a function like `def func(): x = 3; return x` is called, it will create a global variable named `x`? – Christian Dean Feb 19 '18 at 16:13
  • Please post a [mcve]. It's hard to say why your code doesn't work without being able to look at it. – Aran-Fey Feb 19 '18 at 16:15
  • Python doesn't have "global" variables in that sense. There's no reason to use them, anyway. – Daniel Roseman Feb 19 '18 at 16:15
  • I have updated the question with a very basic example. – Nate Thompson Feb 19 '18 at 16:17
  • Ok, your example shows two different functions writing to two different variables, so these will obviously work as separate scripts. What symptoms are you seeing when`the global variables are not being returned as a global variable any longer.` – quamrana Feb 19 '18 at 16:30
  • @quamrana The issue I am seeing is that once I turn these files into a package, and I run the first function, the 'new_table' argument isnt available to the second function. I am going to take a look at Paula's post below and see if that solves it for me. – Nate Thompson Feb 19 '18 at 16:51
  • You don't mention the `new_table` global from function1 in function2. Oh, except you use it as a parameter. Perhaps your call site should use `global new_table` and then pass it: `function2(new_table)` – quamrana Feb 19 '18 at 16:54

1 Answers1

5

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

Pauloba
  • 566
  • 2
  • 14
  • To add, "global" variables in Python are global to a module. When any Python code executes it executes within some namespace for global variables, and that is, in almost all cases, the module in which that code was run (in the interpreter prompt that's a special module called `__main__`). – Iguananaut Feb 19 '18 at 16:37