-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathEnum_tutorial.py
More file actions
41 lines (32 loc) · 867 Bytes
/
Enum_tutorial.py
File metadata and controls
41 lines (32 loc) · 867 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from enum import Enum, unique
class Color(Enum):
red = 1
green = 2
blue = 3
# @unique # 不能重複定義
# class ColorDuplicate(Enum):
# red = 1
# green = 2
# blue = 2
def example_1():
print('Color.red:', Color.red)
print('repr(Color.red):', repr(Color.red))
print('Color(1):', Color(1))
member = Color.red
print('member.name:', member.name)
print('member.value:', member.value)
def example_2():
animal = Enum('Animal', 'ant bee cat dog') # <1>
# is equivalent to
# >>> class Animal(Enum):
# ... ant = 1
# ... bee = 2
# ... cat = 3
# ... dog = 4
print('animal:', animal)
print('animal.ant:', animal.ant)
print('animal.ant.value:', animal.ant.value)
print('list(animal):', list(animal))
if __name__ == "__main__":
example_1()
# example_2()