Python Forum
Sharing global variables between script in python
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sharing global variables between script in python
#1
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
-----------------------------------------
Reply
#2
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'
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
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
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to secure API key when sharing quarinteen 2 338 Jan-19-2024, 04:46 PM
Last Post: deanhystad
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,181 Jun-29-2023, 11:57 AM
Last Post: gologica
  Trying to understand global variables 357mag 5 1,113 May-12-2023, 04:16 PM
Last Post: deanhystad
  Why Pip is not listed in the official Global Python Module index? quazirfan 2 755 Apr-21-2023, 10:55 AM
Last Post: snippsat
  Global variables or local accessible caslor 4 1,018 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,139 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 942 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  How to output Python in Spout or any other video sharing network? buzzdarkyear 4 2,140 Jan-11-2022, 11:37 AM
Last Post: buzzdarkyear
  How to install modules for 2.7 (or sharing from 3.8)) Persisto 2 2,431 Dec-31-2021, 02:33 PM
Last Post: Persisto
  multiprocessing and sharing object cyrduf 0 2,043 Feb-02-2021, 08:16 PM
Last Post: cyrduf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020