Python3-enum枚举类使用示例

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
from enum import Enum

# 声明枚举类
class Branch(Enum):
MAIN = "main branch"
DEV = "develop branch"


print(Branch.MAIN.name) # MAIN
print(Branch.MAIN.value) # main branch
print(Branch["MAIN"]) # Branch.MAIN
print(Branch("main branch")) # Branch.MAIN
print(Branch.MAIN) # Branch.MAIN

print(Branch.MAIN == Branch.MAIN) # True
print(Branch.MAIN == Branch.DEV) # False

print(Branch.MAIN is Branch.MAIN) # True

for branch in Branch:
print(branch)
# Branch.MAIN
# Branch.DEV

for branch in Branch.__members__:
print(branch)
# MAIN
# DEV
作者

Etsu

发布于

2021-10-28

更新于

2021-10-28

许可协议

评论