5

OK so the project I am working on requires* using a truly global variable (or five) and the other questions on here have to do with cross module variables (could be helpful later just not yet).

Here is my problem: I have a program that will be running an environmental simulation and I need to set some global variables to be used and modified inside some of the functions. How would one go about doing that?

*When I say require I mean that I haven't found a more efficient way to do this.

7
  • I'd probably use memcached or redis for something like this. Commented Nov 7, 2014 at 1:23
  • What does "truly global" mean if not "cross-module (like built-in) global"? You mean shared between processes? Shared between separate runs at different times? Shared between every copy of the program running on any computer in the world? Spherical, with a map projected on them? Commented Nov 7, 2014 at 1:39
  • As always it depends, but there are literally 3 cases in software development where global variable is the right answer. What exactly is preventing you from just passing the relevant data from one function to another? Commented Nov 7, 2014 at 1:43
  • What I am asking is if there is a way to make a variable accessible to all functions in one program. Commented Nov 7, 2014 at 1:44
  • 1
    Why do you believe that your program requires global variables? Sure, globals may be convenient, but they mess up modular design (and testing). You should probably be using a class to store the state parameters of your environmental simulation. Commented Nov 7, 2014 at 2:17

2 Answers 2

4

Yes, there is a way to use and modify global variables inside functions that are spread across several modules. Put the variables in a module and import that module into your main program:

# globals.py
klaatu = "Once in Persia reigned a king ..."

 

# main_program.py
import globals

def modify_klaatu():
    print globals.klaatu
    globals.klaatu = "It was the best of times, it was the worst of times."
Sign up to request clarification or add additional context in comments.

2 Comments

Do you know if this also works when multithreading?
This doesn't work if there is another module that also imports globals. That module will have a separate copy of globals, which won't be changed by the modify_klaatu function, which only modifies main_program.globals.klaatu.
1

A somewhat dodgy but entirely dynamic variant of @rob's approach:

import types
import sys

sys.modules['myglobals'] = types.ModuleType('myglobals')
sys.modules['myglobals'].__dict__.update({'first': 'thing', 'second': 'thing'})

import myglobals
print(myglobals.first) # prints 'thing'

Or if you've only got one global, how about this extremely dodgy approach:

import sys
sys.modules['myglobal'] = 'thing'

import myglobal
print(myglobal) # prints 'thing'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.