开发者

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

开发者 https://www.devze.com 2023-03-25 04:36 出处:网络
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.
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 a开发者_StackOverflow中文版ny 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.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 *.


Couldn't you just have a file with the constants, and call:

from file_constants import *

..?


I would suggest having an object that stores your constants. For example, something like this:

# file_one.py
class _COLORS:
    BLUE = 0
    RED = 1
    GREEN = 2
COLORS = _COLORS()

Then you can reference it like this:

# file_two.py
from file_one import COLORS
print COLORS.BLUE #prints 0


You could do this:

class _Constants(object): pass

constants = _Constants()
constants.DEFINE1 = 1
constants.DEFINE2 = 2
...

Then to import these, do (leave off the '.py' from the import):

from file_one import constants

This also allows you to group your constants into meaningful subsets too:

colors = _Constants()
colors.RED = (255,0,0)
colors.GREEN = (0,255,0)
colors.BLACK = (0,0,0)
colors.WHITE = (255,255,255)

linetypes = _Constants()
linetypes.SOLID = 0
linetypes.DOTTED = 1
linetypes.DASHED = 2

And just import colors or linetypes. This will give your constants something of a namespace within your module, so that you can limit your imports, and avoid some name clashing.

0

精彩评论

暂无评论...
验证码 换一张
取 消