from enum import Enum
class Color(Enum):
RED = 1
BLUE = 2
GREEN = 3
print(Color.RED.name) # Output: "RED"
print(Color.RED.value) # Output: "1"
enumeration:Colorenumeration members:RED, BLUE, GREEN 成員的名稱按照慣例為大寫,因為它們被視為常數。每個 enumeration member 都有兩個重要的屬性:name 和 value。 name 屬性給出 member name (string),value 屬性給出成員的值。
Enum members, values
有兩個同名的 enum members 是無效的。但是,兩個 enum members 允許具有相同的值。 給定兩個具有相同值的 members A 和 B(並且 A 首先定義),member B 是 A 的別名。
PYTHON
import enum
class Shape(enum.Enum):
DIAMOND = 1
SQUARE = 2
CIRCLE = 3
ALIAS_FOR_SQUARE = 2
print(Shape.SQUARE) # Output: Shape.SQUARE
print(Shape.ALIAS_FOR_SQUARE) # Output: Shape.SQUARE
print(Shape(2)) # Output: Shape.SQUARE
class Shape(enum.Enum):
SQUARE = 2
SQUARE = 3
# Output:
# TypeError: 'SQUARE' already defined as 2
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
YELLOW = 4
# Accessing value of Enum member
print(Color.RED.value) # Output: 1
print(Color.RED.name) # Output: RED
# Accessing Enum member based on its value
print(Color(1)) # Output: Color.RED
# Accessing Enum member
print(Color.RED) # Output: Color.RED
print(Color['RED']) # Output: Color.RED
# =========================================================
print(repr(Color.RED)) # Output: <Color.RED: 1>
print(list(Color)) # Output: [<Color.RED: 1>, <Color.GREEN: 2>, <Color.BLUE: 3>]
print(list(reversed(Color))) # Output: [<Color.BLUE: 3>, <Color.GREEN: 2>, <Color.RED: 1>]
print(dir(Color)) # Output: ['BLUE', 'GREEN', 'RED', '__class__', '__doc__', '__members__', '__module__']
print(len(Color)) # Output: 3
print(type(Color)) # <class 'enum.EnumType'>
print(type(Color.RED)) # <enum 'Color'>
print(Color.RED in Color) # Output: True
print(isinstance(Color.GREEN, Color)) # Output: True
# =========================================================
print(Color.RED is Color.BLUE) # Output: False
print(Color.RED is not Color.BLUE) # Output: True
print(Color.RED == Color.BLUE) # Output: False
print(Color.RED != Color.BLUE) # Output: True
# Enum members are not integers
print(Color.RED == 1) # Output: False
#print(Color.RED < Color.BLUE)
#print(Color.RED <= Color.BLUE)
# =========================================================
# Iterating Enum
for i in Color:
print(i)
'''
Color.RED
Color.GREEN
Color.BLUE
Color.YELLOW
'''
for name, member in Color.__members__.items():
print(name, member)
'''
RED Color.RED
GREEN Color.GREEN
BLUE Color.BLUE
YELLOW Color.YELLOW
'''
# Used for detailed programmatic access
print([name for name, member in Color.__members__.items() if member.name != None])
# Output: ['RED', 'GREEN', 'BLUE', 'YELLOW']
'''
names = []
for name, member in Color.__members__.items():
if member.name is not None:
names.append(name)
'''
# =========================================================
# Enumeration members are hashable
# Use Enum members as dictionary keys
color_dict = {
Color.RED: 'It symbolizes love and anger.',
Color.GREEN: 'It symbolizes nature and tranquility.',
Color.BLUE: 'It symbolizes sea and sky.',}
print(color_dict[Color.RED]) # Output: 'It symbolizes love and anger.'
# Use Enum members in sets
color_set = {Color.RED, Color.BLUE}
print(Color.GREEN in color_set) # Output: False
print(Color.RED in color_set) # Output: True
Enum
Enum 是 enum enumerations 的 Base Class。
EnumType 負責在其他 enum Class 上設置正確的方法,包含: name, value, _generate_next_value_, __str__ 方法...。
from enum import IntEnum, Enum
class Shape(IntEnum):
CIRCLE = 1
SQUARE = 2
class Request(IntEnum):
POST = 1
GET = 2
print(Shape.CIRCLE == 1) # Output: True
print(Shape.CIRCLE == Request.POST) # Output: True
# =========================================================
class Color(Enum):
RED = 1
GREEN = 2
print(Shape.CIRCLE == Color.RED) # Output: False
# =========================================================
# IntEnum To int
print(int(Shape.CIRCLE)) # Output: 1
# IntEnum To index
print(['a', 'b', 'c'][Shape.CIRCLE]) # Output: "b"
# IntEnum To len()
print([i for i in range(Shape.SQUARE)]) # Output: [0, 1]