我使用Enum4库创建一个枚举类如下:
class Color(Enum):
RED = 1
BLUE = 2
我想把[1,2]作为一个列表打印出来。我怎样才能做到这一点呢?
我使用Enum4库创建一个枚举类如下:
class Color(Enum):
RED = 1
BLUE = 2
我想把[1,2]作为一个列表打印出来。我怎样才能做到这一点呢?
当前回答
使用__members__类方法:
class RoleNames(str, Enum):
AGENT = "agent"
USER = "user"
PRIMARY_USER = "primary_user"
SUPER_USER = "super_user"
@classmethod
def list_roles(cls):
role_names = [member.value for role, member in cls.__members__.items()]
return role_names
>>> role_names = RoleNames.list_roles()
>>> print(role_names)
或者如果你有多个Enum类并且想要抽象类方法:
class BaseEnum(Enum):
@classmethod
def list_roles(cls):
role_names = [member.value for role, member in cls.__members__.items()]
return role_names
class RoleNames(str, BaseEnum):
AGENT = "agent"
USER = "user"
PRIMARY_USER = "primary_user"
SUPER_USER = "super_user"
class PermissionNames(str, BaseEnum):
READ = "updated_at"
WRITE = "sort_by"
READ_WRITE = "sort_order"
其他回答
使用__members__类方法:
class RoleNames(str, Enum):
AGENT = "agent"
USER = "user"
PRIMARY_USER = "primary_user"
SUPER_USER = "super_user"
@classmethod
def list_roles(cls):
role_names = [member.value for role, member in cls.__members__.items()]
return role_names
>>> role_names = RoleNames.list_roles()
>>> print(role_names)
或者如果你有多个Enum类并且想要抽象类方法:
class BaseEnum(Enum):
@classmethod
def list_roles(cls):
role_names = [member.value for role, member in cls.__members__.items()]
return role_names
class RoleNames(str, BaseEnum):
AGENT = "agent"
USER = "user"
PRIMARY_USER = "primary_user"
SUPER_USER = "super_user"
class PermissionNames(str, BaseEnum):
READ = "updated_at"
WRITE = "sort_by"
READ_WRITE = "sort_order"
这里有一些例子,可以轻松地将Enum转换为int, str或Enum的列表/数组,并能够排序。
import numpy as np
class Color(int,Enum):
YELLOW = 3
RED = 1
BLUE = 2
print('1):',list(Color))
print('2):',np.array(list(Color))) ## int64 (8 bytes)
print('3):',sorted(np.array(Color, dtype=str)))
print('4):',np.array(sorted(Color), dtype=object))
print('5):',np.array(sorted(Color), dtype=np.int8)) # 1 byte
print('6):',np.array(sorted(Color, key=lambda x: -x.value), dtype=np.int8))
print('7):',np.array(sorted(Color, key=lambda x: str(x)), dtype=np.int8))
class Color(tuple,Enum):
YELLOW = (3,3)
RED = (1,1)
BLUE = (2,2)
print('8):',np.array(sorted(Color)))
print('9):',list(map(tuple,sorted(Color, key=lambda x: -x[1]))))
输出:
1): [<Color.YELLOW: 3>, <Color.RED: 1>, <Color.BLUE: 2>]
2): [3 1 2]
3): ['Color.BLUE', 'Color.RED', 'Color.YELLOW']
4): [<Color.RED: 1> <Color.BLUE: 2> <Color.YELLOW: 3>]
5): [1 2 3]
6): [3 2 1]
7): [2 1 3]
8): [[1 1]
[2 2]
[3 3]]
9): [(3, 3), (2, 2), (1, 1)]
基于@Jeff的回答,重构为使用类方法,以便您可以对任何枚举重用相同的代码:
from enum import Enum
class ExtendedEnum(Enum):
@classmethod
def list(cls):
return list(map(lambda c: c.value, cls))
class OperationType(ExtendedEnum):
CREATE = 'CREATE'
STATUS = 'STATUS'
EXPAND = 'EXPAND'
DELETE = 'DELETE'
print(OperationType.list())
生产:
['CREATE', 'STATUS', 'EXPAND', 'DELETE']
你可以使用IntEnum:
from enum import IntEnum
class Color(IntEnum):
RED = 1
BLUE = 2
print(int(Color.RED)) # prints 1
获取整型数组的列表:
enum_list = list(map(int, Color))
print(enum_list) # prints [1, 2]
给定一个基于标准python3 enum /IntEnum类的枚举:
from enum import IntEnum
class LogLevel(IntEnum):
DEBUG = 0
INFO = 1
WARNING = 2
ERROR = 3
可以执行以下操作来获取枚举常量列表:
>>> print(list(LogLevel))
[<LogLevel.DEBUG: 0>, <LogLevel.INFO: 1>, <LogLevel.WARNING: 2>, <LogLevel.ERROR: 3>]
我发现使用枚举常量比使用int型更有表现力。如果枚举从IntEnum继承,则所有枚举常量也是int型,并且可以在任何地方使用:
>>> level = LogLevel.DEBUG
>>> level == 0
True
>>> level == 1
False
>>> level == LogLevel.INFO
False
>>> level == LogLevel.DEBUG
True
>>> "%d" % level
'0'
>>> "%s" % level
'LogLevel.DEBUG'