classEnumMeta(type): def__new__(metacls, cls, bases, classdict): ... enum_class._member_names_ = [] names in definition order enum_class._member_map_ = OrderedDict() name->value map @property def__members__(cls): """Returns a mapping of member name->value. This mapping lists all enum members, including aliases. Note that this is a read-only view of the internal mapping. """ return MappingProxyType(cls._member_map_)
所有写法有:
通过类的value属性来获得所有值
1 2 3 4 5 6 7 8 9 10 11 12 13
from enum import Enum classColor(Enum): RED = 1 GREEN = 'GREEN' BLUE = ('blue', '#0000ff') @staticmethod deflist(): ''' 获得所有值 ''' return list(map(lambda c: c.value, Color)) print(Color.list())
模仿Java写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classChess(Enum): BLACK = {'graph': 'X', 'val': 0} WHITE = {'graph': 'O', 'val': 1}
defgetChess(color): for c in Chess: if color == c.value.get("graph"): return c