views:

84

answers:

6

I'm writing an application in Python, and I've got a number of universal variables (such as the reference to the main window, the user settings, and the list of active items in the UI) which have to be accessible from all parts of the program1. I only just realized I've named the module globals.py and I'm importing the object which contains those variables with a from globals import globals statement at the top of my files.

Obviously, this works, but I'm a little leery about naming my global object the same as the Python builtin. Unfortunately, I can't think of a much better naming convention for it. global and all are also Python builtins, universal seems imprecise, state isn't really the right idea. I'm leaning towards static or env, although both have a specific meaning in computer terms which suggests a different concept.

So, what (in Python) would you call the module which contains variables global to all your other modules?

1 I realize I could pass these (or the single object containing them) as a variable into every other function I call. This ends up being infeasible, not just because it makes the startup code and function signatures really ugly.

A: 

global is a keyword, not a built-in. 'globals' is not a keyword, but is a built-in function. It can be assigned to, but is bad practice. Code checkers like pylint and pychecker can catch these accidental assignments. How about config?

Mark Tolonen
globals() is a built in function.
Max Shawabkeh
A: 

top? top_level?

Michał Marczyk
+3  A: 

I would try to avoid such a global container module altogether, and instead put these variables into their own modules, which can then be imported from all parts of the system.

For example, the main window would probably go into a variable in main.py. User settings could go into usersettings.py which would provide functions to view and change the settings.

If another part of the system needs to access the user settings, that's a simple matter of:

from usersettings import get_setting, set_setting
...
# Do stuff with settings

A similar approach could probably be used for other stuff that needs to be globally accessible. This leads to clearer separation of concerns and more testable code, since you can test modules in isolation without depending on the globals module all the time.

Pär Wieslander
Naming fun aside, this is probably the best course of action... +1.
Michał Marczyk
I do have those pieces that make sense broken off into their own separate module, to be imported separately. But there's a lot of objects which just need to be accessible everywhere. As the number of global variables multiplies, I start needing to import every module from every other module. And then I start to run into ordering problems, where I start having to move code into different modules so it can be imported separately by two other modules without affecting each other.Having a single global module greatly reduces the complexity of the code, with very little tradeoff.
Chris B.
+1  A: 
`config` or `settings`
Aadith
+1  A: 

I'd call it env. There's little risk that someone will confuse it with os.environ (especially if you organize your code so that you can call it myapp.environ).

I'd also make everything exposed by myapp.environ a property of a class, so that I can put breakpoints in the setter when the day comes that I need to.

Robert Rossney
A: 
from globals import Globals

This will fix the conflict and also follows PEP 8 recommendations.

Also, in other cases like this, Roget's Thesaurus is your friend. I always keep a copy nearby.

Don O'Donnell