python 高階語法與用法-列舉的簡單用法
阿新 • • 發佈:2020-08-07
1、列舉
- 可讀性更強
from enum import Enum class VIP(Enum): YELLOW = 1 GREEN = 2 BALCK = 3 RED = 4 print(VIP.BALCK) print(type(VIP.BALCK)) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test17.py" # VIP.BALCK # <enum 'VIP'>
按照普通類來分析,類名.類變數 ,應該輸出的是3,但是其實卻是VIP.BALCK。
先不解釋為什麼。先說說,其實這種列印結果應該對呼叫該類的業務方來說,是更希望看到VIP.BALCK的吧,而不是一個數字3,也不知道這個意義是啥。
這就是說,列舉類不能按照普通類來分析。
接下來說說列舉類的特點和優勢
- 列舉類中的value不可變:Cannot reassign members;業務需求中,往往也需要定義的常量型別,是不允許被修改的。
-
from enum import Enum class VIP(Enum): YELLOW = 1 GREEN = 1 BALCK = 3 RED = 4 print(VIP.GREEN) VIP.GREEN
用普通類代替列舉類,被輕易修改class Common(): GREEN = 1 Common.GREEN = 3 print(Common.GREEN) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test17.py" # 3
dict來替代列舉類,被輕易修改
a = {"GREEN":1,"RED":4} a["GREEN"] = 3 print(a) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test17.py" # {'GREEN': 3, 'RED': 4}
- 列舉類中不能存在相同的標籤名
-
from enum import Enum class Animal(Enum): CAT = 1 CAT = 2 # anson@ansonwandeMacBook-Pro python_ToolCodes % python3 test19.py # Traceback (most recent call last): # File "test19.py", line 3, in <module> # class Animal(Enum): # File "test19.py", line 5, in Animal # CAT = 2 # File "/usr/local/Cellar/[email protected]/3.8.3_2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/enum.py", line 95, in __setitem__ # raise TypeError('Attempted to reuse key: %r' % key) # TypeError: Attempted to reuse key: 'CAT'
需要注意的是,python2.7版本是允許有相同名稱的標籤的,但是目前所學的python3.8時不允許有相同名稱的標籤的
- 獲取列舉的標籤和數值,該怎麼操作VIP.YELLOW.name,VIP.YELLOW.value
-
from enum import Enum #coding=utf-8 class VIP(Enum): YELLOW = 1 GRAY = 2 BALCK = 3 RED = 4 print(VIP.YELLOW) print(VIP.YELLOW.name) print(VIP.YELLOW.value) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test17.py" # VIP.YELLOW # YELLOW # 1
- 列舉遍歷
from enum import Enum #coding=utf-8 class VIP(Enum): YELLOW = 1 GRAY = 2 BALCK = 3 RED = 4 for v in VIP: print(v) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test17.py" # VIP.YELLOW # VIP.GRAY # VIP.BALCK # VIP.RED
- 列舉比較
-
“==”比較:允許
from enum import Enum #coding=utf-8 class VIP(Enum): YELLOW = 1 GRAY = 2 BALCK = 3 RED = 4 print(VIP.BALCK == VIP.GRAY) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test17.py" # False
>,<比較:不允許!!!from enum import Enum #coding=utf-8 class VIP(Enum): YELLOW = 1 GRAY = 2 BALCK = 3 RED = 4 print(VIP.BALCK > VIP.GRAY) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test17.py" # Traceback (most recent call last): # File "/Users/anson/Documents/Project/python_ToolCodes/test17.py", line 9, in <module> # print(VIP.BALCK > VIP.GRAY) # File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/enum/__init__.py", line 740, in __gt__ # raise TypeError("unorderable types: %s() > %s()" % (self.__class__.__name__, other.__class__.__name__)) # TypeError: unorderable types: VIP() > VIP()
is 比較:允許from enum import Enum #coding=utf-8 class VIP(Enum): YELLOW = 1 GRAY = 2 BALCK = 3 RED = 4 print(VIP.BALCK is VIP.BALCK) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test17.py" # True
列舉類之間比較:允許
from enum import Enum #coding=utf-8 class VIP1(Enum): YELLOW = 1 GRAY = 2 BALCK = 3 RED = 4 class VIP2(Enum): YELLOW = 1 GRAY = 2 BALCK = 3 RED = 4 print(VIP1.GRAY is VIP2.GRAY) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test17.py" # False
-