Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import large number of global variables without listing each one? (Python)

Tags:

python

import

DEFINE1 = 1
DEFINE2 = 2
DEFINE3 = 3
...
DEFINE10 = 10

Let's say one file has 10 global constants that I want to import into another file.

Instead of doing the following, is there any simpler way to import all the global constants without doing something like: from file_one.py import *. I don't want it to import the entire file, just the global variables.

from file_one.py import DEFINE1, DEFINE2, DEFINE3, ..............
like image 260
Takkun Avatar asked Aug 03 '11 15:08

Takkun


People also ask

Can you import global variables in Python?

A global variable is a variable which is accessible in multiple scopes. In Python, it is better to use a single module to hold all the global variables you want to use and whenever you want to use them, just import this module, and then you can modify that and it will be visible in other modules that do the same.

How do I share global variables across modules in Python?

See Python's document on sharing global variables across modules: The canonical way to share information across modules within a single program is to create a special module (often called config or cfg). Import the config module in all modules of your application; the module then becomes available as a global name.

How do you access a global variable across a file in Python?

To use global variables between files in Python, we can use the global keyword to define a global variable in a module file. Then we can import the module in another module and reference the global variable directly. We import the settings and subfile modules in main.py . Then we call settings.

Is it good to use global variables Python?

The reason global variables are bad is that they enable functions to have hidden (non-obvious, surprising, hard to detect, hard to diagnose) side effects, leading to an increase in complexity, potentially leading to Spaghetti code.


2 Answers

First of all, I think it's perfectly okay to create a lot of constants like you're doing and put them all in a constants.py file and then do a from constants import * - I do this myself all the time. So long as all of my constants are defined in that file, I know exactly where to look when I need to figure out where SOME_CONSTANT came from.

But I'll assume for the moment that you have a module with a lot of constants and they all consist of upper-case letters, numbers, and underscores. At that point you can do something very hackish like

import re, file_one
for name,val in file_one.__dict__.items():
    if re.match("[A-Z0-9_]+", name):
        globals()[name] = val

I would strongly advise against this sort of hackery, but this would make it possible to automatically import the constants you define without having to list them individually.

like image 183
Eli Courtwright Avatar answered Oct 06 '22 06:10

Eli Courtwright


Assuming that all upper-case names are global constants:

import file_one
g = globals()
for key in dir(file_one):
    if key.isupper():
        g[key] = getattr(file_one, key)

Or shorter:

import file_one
globals().update((key, getattr(file_one, key)) for key in dir(file_one)
                                               if key.isupper())

Having said that: Don't do it. Explicit is better than implicit. You should put these constants in a separate module and import them in both modules using from constants import *.

like image 23
Ferdinand Beyer Avatar answered Oct 06 '22 07:10

Ferdinand Beyer