在JavaScript中,我们可以这样做:
var Color = {
YELLOW: { value: 1, displayString: "Yellow" },
GREEN: { value: 2, displayString: "Green" },
}
所以我可以致电:
Color.YELLOW.displayString
在Java中我们可以这样做:
public enum Color {
YELLOW (1, "Yellow"),
GREEN (2, "Green"),
private Color(String value, int displayString){
this.value = value;
this.displayString = displayString;
}
private final int value;
private final String displayString;
public String getValue() {return value;}
public String getDisplayString() {return displayString;}
}
所以我可以致电:
Color.YELLOW.getDisplayString()
经过大量研究后,我发现使用内置的Enum模块在Python中找不到干净的方法。我怎么能这样做?
由于
答案 0 :(得分:15)
这里涉及两个概念:枚举和对象成员的属性样式访问,可以内联初始化。对于后者,你需要某种自定义类,但由于你想要一些非常直接的东西,namedtuple
就足够了。因此,结合namedtuple
和enum
,这可能是一个解决方案:
from enum import Enum
from collections import namedtuple
Color = namedtuple('Color', ['value', 'displayString'])
class Colors(Enum):
@property
def displayString(self):
return self.value.displayString
yellow = Color(1, 'Yellow')
green = Color(2, 'Green')
print(Colors.yellow.displayString)
答案 1 :(得分:2)
这是另一种方法,即来自https://github.com/hzdg/django-enumfields
的代码import enum
import inspect
class ColorEnumMeta(enum.EnumMeta):
def __new__(mcs, name, bases, attrs):
DisplayStrings = attrs.get('DisplayStrings')
if DisplayStrings is not None and inspect.isclass(DisplayStrings):
del attrs['DisplayStrings']
if hasattr(attrs, '_member_names'):
attrs._member_names.remove('DisplayStrings')
obj = super().__new__(mcs, name, bases, attrs)
for m in obj:
m.display_string = getattr(DisplayStrings, m.name, None)
return obj
class Color(enum.Enum, metaclass=ColorEnumMeta):
yellow = 1
green = 2
class DisplayStrings:
yellow = 'Yellow'
green = 'Green'
print(Color.yellow.display_string) # 'Yellow'
或基于此代码的东西,但稍微短一些:
import enum
class ColorEnumMeta(enum.EnumMeta):
def __new__(mcs, name, bases, attrs):
obj = super().__new__(mcs, name, bases, attrs)
obj._value2member_map_ = {}
for m in obj:
value, display_string = m.value
m._value_ = value
m.display_string = display_string
obj._value2member_map_[value] = m
return obj
class Color(enum.Enum, metaclass=ColorEnumMeta):
yellow = 1, 'Yellow'
green = 2, 'Green'
print(Color.yellow.display_string) # 'Yellow'
答案 2 :(得分:0)
您可以使用dictionary in python:
Color = {
'YELLOW': { 'value': 1, 'displayString': "Yellow" },
'GREEN': { 'value': 2, 'displayString': "Green" }
}
Color['YELLOW']['displayString']
答案 3 :(得分:0)
您通常不应在代码中包含这样的文字。内容应来自文件或数据库。但你可以看到如何构建它
>>> from collections import namedtuple
>>> Color = namedtuple("color", "YELLOW,GREEN")(
namedtuple("yellow", "value,display_string")(1, "yellow"),
namedtuple("green", "value,display_string")(2, "green"))
>>> Color.YELLOW.display_string
'yellow'
这也适用于不支持枚举
的旧蟒蛇