我有一个python文件,其中包含项目中每一个其他类的所有默认值
# config.py
DEFAULT_WORKER_TYPE = 'A'
DEFAULT_METRIC_TYPE = 'euclid'
...
# worker.py
import config
class Worker:
def __init__(self, worker_type=config.DEFAULT_WORKER_TYPE):
pass
# metric.py
import config
class Metric:
def __init__(self, metric_type=config.DEFAULT_METRIC_TYPE):
pass
这个很好,但是我想用
Enum
对于
worker_type
和
metric_type
而不是原始字符串以避免输入错误
# config.py
from worker import WorkerType
from metric import MetricType
DEFAULT_WORKER_TYPE = WorkerType.A
DEFAULT_METRIC_TYPE = MetricType.EUCLID
# worker.py
from enum import Enum
import config
class WorkerType(Enum):
A = 'A'
B = 'B'
class Worker: # the same
# metric.py
from enum import Enum
import config
class MetricType(Enum):
EUCLID = 'euclid'
MANHATTAN = 'manhattan'
class Metric: # the same
现在,在我看来,后一个版本不是很有效,因为它有某种循环导入,如果我的主程序只使用一个文件(
worker.py
例如,它仍然需要每隔一个文件导入一次(
metric.py
因为
config.py
全部进口。
有没有更好的方法来实现这个目标?
我将所有默认值移动到
配置py
我需要经常用这些值做实验。把所有东西放在一个地方可以帮助我不必记住要更改哪个文件。