研究了一下文件, 沒看到能剛好 fit 手邊需求的用法@@...
以官方文件的示範為例,建立一個 Enum
:
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color._member_map_)
# {'RED': <Color.RED: 1>, 'GREEN': <Color.GREEN: 2>, 'BLUE': <Color.BLUE: 3>}
如果想要增加一個 ClassVar _favorite
紀錄某 些 member,
例如:
class Color2(Enum):
RED = 1
GREEN = 2
BLUE = 3
_favorite = {RED, BLUE}
print(Color2._member_map_)
# {'RED': <Color2.RED: 1>, 'GREEN': <Color2.GREEN: 2>, 'BLUE': <Color2.BLUE: 3>, '_favorite': <Color2._favorite: {1, 3}>}
但是 _favorite
會被當做 enum member ...