Oct-27-2016, 08:00 AM
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
-----------------------------------------
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
-----------------------------------------