Python Forum
Sharing global variables between script in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Sharing global variables between script in python (/thread-662.html)



Sharing global variables between script in python - capponero - Oct-27-2016

Hello guys,

I have just moved from Matlab to Python (I am using Spyder environment) and I am depressed that I cannot share the global variables between script. I would like to have in the same folder one file, where all functions are defined, including parameters (global variables) and to use those functions in every script in the folder, where are the parameters defined. So I would like to have something like this:
-----------------------------------------
file: funs.py:

def my_fun(x):
   return(x + A)
-----------------------------------------
file: my_script1.py:

import funs

A=5
print(funs.my_fun(3))
#this will return me result 8
-----------------------------------------
file: my_script2.py:

import funs

A=6
print(funs.my_fun(3))
#this will return me result
-----------------------------------------

in Matlab I simply defined in every function that A is global and I dind't have to define its value in the functions file. It took the appropriate value from each script, where A was also defined as global. However in Python it is not enough, since it always display an error that A is not defined. I want to have the A to be different for every script, where I call the function, but I don't want to have A as variable, but parameter instead.

Any suggestions or advises?

Thanks a lot in advance!

correction:

-----------------------------------------
file: my_script2.py:

import funs

A=6
print(funs.my_fun(3))
#this will return me result 9
-----------------------------------------


RE: Sharing global variables between script in python - Ofnuts - Oct-27-2016

Global variables are evil in a single module, and they would be even more evil if shared between modules.

What you can do is have a "settings" modules in which you define the variables. Then
  • all modules do an import of settings and use settings.var1, settings.var2, etc...
  • one module (main) can set variables values in it: settings.var1='foobar'



RE: Sharing global variables between script in python - sparkz_alot - Oct-27-2016

Could he not just add an additional parameter to the my_fun function?

def my_fun(x, some_var):
    return(x + some_var)
any other files:

import funs

A = 5    # Or what ever you want to call it.
print(funs.my_fun(3, A))
I don't understand why that would be 'evil'.  Just curious Think


RE: Sharing global variables between script in python - nilamo - Oct-27-2016

(Oct-27-2016, 12:49 PM)sparkz_alot Wrote: I don't understand why that would be 'evil'.  Just curious Think

If you're looking through a file, the last thing you'd expect is that a completely different file is going to be modifying variables.  Calling functions, creating objects, sure, but modifying variables?  That's an easy way for bugs to creep silently into your codebase.