import enum
class SETTING(enum.Enum):
png = 0
jpg = 1
bmp = 2
psd = 3
tga = 4
print(SETTING.png)
print(SETTING.png.value)
print(SETTING.png.name)
SETTING.png 0 png
from enum import Enum
class Color(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
print(Color.RED) # Color.RED
print(Color.RED.value) # 'red'
.value アトリビュートで、Enumメンバーの実際の文字列値にアクセスできる。
文字列からEnumメンバーを取得するには以下の通り。
color_name = "green"
color = Color[color_name.upper()]
print(color) # Color.GREEN
from enum import Enum
class Color(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
color_list = [color for color in Color]
print(color_list)
# [<Color.RED: 'red'>, <Color.GREEN: 'green'>, <Color.BLUE: 'blue'>]
value_list = [color.value for color in Color]
print(value_list)
# ['red', 'green', 'blue']