Evi*_*sha 8 python enums python-class
我正在寻找一种Python式的方法来在单个文件中定义多个相关常量以在多个模块中使用。我想出了多种选择,但它们都有缺点。
# file resources/resource_ids.py
FOO_RESOURCE = 'foo'
BAR_RESOURCE = 'bar'
BAZ_RESOURCE = 'baz'
QUX_RESOURCE = 'qux'
Run Code Online (Sandbox Code Playgroud)
# file runtime/bar_handler.py
from resources.resource_ids import BAR_RESOURCE
# ...
def my_code():
value = get_resource(BAR_RESOURCE)
Run Code Online (Sandbox Code Playgroud)
这很简单且通用,但有一些缺点:
_RESOURCE必须附加到所有常量名称以提供上下文# file resources/resource_ids.py
from enum import Enum, unique
@unique
class ResourceIds(Enum):
foo = 'foo'
bar = 'bar'
baz = 'baz'
qux = 'qux'
Run Code Online (Sandbox Code Playgroud)
# file runtime/bar_handler.py
from resources.resource_ids import ResourceIds
# ...
def my_code():
value = get_resource(ResourceIds.bar.value)
Run Code Online (Sandbox Code Playgroud)
这解决了第一种方法的问题,但该解决方案的缺点是需要使用.value来获取字符串表示形式(假设我们需要字符串值而不仅仅是一致的枚举值)。未能附加.value可能会导致运行时难以调试问题。
# file runtime/bar_handler.py
from resources.resource_ids import ResourceIds
# ...
def my_code():
value = get_resource(ResourceIds.bar.value)
Run Code Online (Sandbox Code Playgroud)
# file runtime/bar_handler.py
from resources.resource_ids import ResourceIds
# ...
def my_code():
value = get_resource(ResourceIds.bar)
Run Code Online (Sandbox Code Playgroud)
这种方法是我最喜欢的,但它可能会被误解——类是为了实例化而设计的。虽然使用类的实例而不是类本身不会影响代码的正确性,但我希望避免这种浪费。
这种方法的另一个缺点是这些值实际上并不是恒定的。任何代码客户端都可能更改它们。
是否可以阻止类被实例化?我是否缺少一些对密切相关的常量进行分组的惯用方式?
Eth*_*man 10
使用Enum并混合str:
@unique
class ResourceIds(str, Enum):
foo = 'foo'
bar = 'bar'
baz = 'baz'
qux = 'qux'
Run Code Online (Sandbox Code Playgroud)
那么你就不需要比较.value:
>>> ResourceIds.foo == 'foo'
True
Run Code Online (Sandbox Code Playgroud)
并且您仍然可以获得良好的调试信息:
>>> ResourceIds.foo
<ResourceIds.foo: 'foo'>
>>> list(ResourceIds.foo.__class__)
[
<ResourceIds.foo: 'foo'>,
<ResourceIds.bar: 'bar'>,
<ResourceIds.baz: 'baz'>,
<ResourceIds.qux: 'qux'>,
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1592 次 |
| 最近记录: |