【发布时间】:2016-07-17 02:25:09
【问题描述】:
我来自 C# 背景,但现在在 Python 3.x 中做了大量的科学计算工作,所以我想了解一下我的风格或口音在多大程度上按照 python 标准是“奇怪的”。
尤其是,Python 中没有 const 这样的东西,这让我感到很痛苦。我的用例是这样的:我正在保存*.npz 文件(numpy 序列化数据字典)、传递字典、写入文件等,并且字典键、文件名等需要具有一致、可靠的命名模式。
很明显,在 8 个地方输入相同的魔法愚蠢字符串是错误的。
所以在我的模块根目录中,我有一个通常称为base.py 的文件:
import os
from collections import namedtuple
from os import path
# This is the tuple that contains operational constants
RuntimeConstants = namedtuple("Const", " ModelDirectoryLabelName \
DefaultModelOutputDirectoryRoot \
ResultDirectoryNameFormat \
PeripheryOutputFilePrefix \
NCOutputFilePrefix \
SummaryPlotFileName \
PeripheryConfigurationName \
ResourceDirectoryName \
StimulusTemplateName")
runtime_consts = RuntimeConstants(ModelDirectoryLabelName=".model-output-root",
DefaultModelOutputDirectoryRoot="model-output",
ResultDirectoryNameFormat="%d %b %y - %H%M",
PeripheryOutputFilePrefix="periphery-output-",
NCOutputFilePrefix="nc-output-",
PeripheryConfigurationName="simulation-configuration.yaml",
SummaryPlotFileName="summary-plots.pdf",
ResourceDirectoryName="resources",
StimulusTemplateName="default_stimulus.yaml"
)
# This is the path of __this file__, which we can then base location on
rootPath = os.path.dirname(os.path.abspath(__file__))
元组是不可变的;命名元组具有语义上有意义的索引,现在:
- 我可以创建多个字典来即时传递数据,但要知道它们的键是什么
- 我可以使用已知文件名和位置写入和检索文件。
- 重构意味着我只需要在一个地方修改一个魔术字符串。
- 即使安装了模块,我也知道我的目录在哪里。
在 c# 中,让一个或多个 Constants 类填充 public static const string Foo = "some magic string value"; 是很正常的做法,所以这就是我在这里尝试重新创建的内容。
我目前在base.py 中有 4 个这样的namedtuples,这似乎已经处于拥有太多的边缘——但我不需要更多。它们在语义上都是不同的——我通过使用关联对常量进行分组。
这是常见的做法吗?
【问题讨论】:
-
这并不常见。同样不常见但可能感兴趣,请参阅问题Can I prevent modifying an object in Python?
标签: python coding-style constants