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, ..............
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.
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.
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.
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.
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.
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 *
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With