【Class 16】列挙
5654 ワード
例1:列挙を定義する
例2:列挙は同じ値(別名)でもよいが、一般的には印刷されない
from enum import Enum
class VIP(Enum):
YELLOW = 1
GREEN = 2
BLACK = 3
RED = 4
print(VIP.YELLOW)
print(VIP.BLACK)
print('--------')
for i in VIP:
print(i)
:
VIP.YELLOW
VIP.BLACK
--------
VIP.YELLOW
VIP.GREEN
VIP.BLACK
VIP.RED
例2:列挙は同じ値(別名)でもよいが、一般的には印刷されない
from enum import Enum
class VIP(Enum):
YELLOW = 1
YELLOW_ALIAS = 1
GREEN = 2
BLACK = 3
RED = 4
# ,
for i in VIP:
print(i)
# , ,
for i in VIP.__members__.items():
print(i)
:
VIP.YELLOW
VIP.GREEN
VIP.BLACK
VIP.RED
('YELLOW', <VIP.YELLOW: 1>)
('YELLOW_ALIAS', <VIP.YELLOW: 1>)
('GREEN', <VIP.GREEN: 2>)
('BLACK', <VIP.BLACK: 3>)
('RED', <VIP.RED: 4>)