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, ..............
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.pyfile and then do afrom 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 whereSOME_CONSTANTcame 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
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.