1. 程式人生 > >python中的列舉

python中的列舉

在python中可以用列舉代表不同的型別。

#列舉在python中是一個類,所有的列舉型別都是Enum類子類#
#列舉是主要作用是打tab,列舉中的tab一般以大寫字母出現#
from enum import Enum

class ComPany(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 2
    LOGISTICS = 3
    MANAGEMENT = 4

print(ComPany.SALESMAN)

#列舉能實現的,字典和普通類其實也能實現#
#但是字典、其他普通類的tab都可以被輕易修改#
from enum import Enum

class ComPany1(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 2
    LOGISTICS = 3
    MANAGEMENT = 4

a = {'SALESMAN':1,'ADMINISTRATIVE':2,'LOGISTICS':3,'MANAGEMENT':4}
a['SALESMAN']=2
print(a)

class ComPany():
    SALESMAN = 1
    ADMINISTRATIVE = 2
    LOGISTICS = 3
    MANAGEMENT = 4

ComPany.SALESMAN = 3
print(ComPany.SALESMAN)

ComPany1.SALESMAN = 3#這裡會報錯,所以列舉類不能被修改#
print(ComPany1.SALESMAN)

#列舉內不會出現相同的tab,但是字典和普通類中都可以出現相同tab#
from enum import Enum

a = {'SALESMAN':1,'SALESMAN':2,'LOGISTICS':3,'MANAGEMENT':4}
print(a)

class ComPany():
    SALESMAN = 1
    SALESMAN = 2
    LOGISTICS = 3
    MANAGEMENT = 4

class ComPany1(Enum):
    SALESMAN = 1
    SALESMAN = 2#這裡會報錯,所以列舉類不能出現相同tab#
    LOGISTICS = 3
    MANAGEMENT = 4

#讀取列舉#
from enum import Enum

class ComPany1(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 2
    LOGISTICS = 3
    MANAGEMENT = 4

print(ComPany1.SALESMAN)#列舉型別#
print(type(ComPany1.SALESMAN))
print('~~~~~~~~~~~~~~~~~~')
print(ComPany1['SALESMAN'])#列舉型別#
print (type(ComPany1['SALESMAN']))
print('~~~~~~~~~~~~~~~~~~')
print (ComPany1.SALESMAN.value)#列舉值#
print (type(ComPany1.SALESMAN.value))
print('~~~~~~~~~~~~~~~~~~')
print (ComPany1.SALESMAN.name)#列舉tab#
print (type(ComPany1.SALESMAN.name))

#遍歷列舉#
class ComPany1(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 2
    LOGISTICS = 3
    MANAGEMENT = 4

for a in ComPany1:
    print(a)

#列舉可以進行等值比較但僅限於列舉tab之間的比較,並且只能進行等值比較不能進行大小比較,也不能進行跨類比較。#
from enum import Enum

class ComPany1(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 1
    LOGISTICS = 3
    MANAGEMENT = 4

a = ComPany1.SALESMAN == ComPany1.ADMINISTRATIVE
print(a)

#在上面那個例項中ComPany1.SALESMAN與ComPany1.ADMINISTRATIVE的值一致,所以ComPany1.ADMINISTRATIVE可以看作是ComPany1.SALESMAN的別名#
#在列印第二個時,出現的會是第一個:#
from enum import Enum

class ComPany1(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 1
    LOGISTICS = 3
    MANAGEMENT = 4

print(ComPany1.ADMINISTRATIVE)

#並且在遍歷時會出現ADMINISTRATIVE消失的情況,因為這時ADMINISTRATIVE已經成為別名了:#
from enum import Enum

class ComPany1(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 1
    LOGISTICS = 3
    MANAGEMENT = 4

for a in ComPany1:
    print(a)

#如果需要所有的tab,我們可以使用__members__下的items方法來遍歷,或者直接使用__members__#
from enum import Enum

class ComPany1(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 1
    LOGISTICS = 3
    MANAGEMENT = 4

for a in ComPany1.__members__.items():
    print(a)


from enum import Enum

class ComPany1(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 1
    LOGISTICS = 3
    MANAGEMENT = 4

for a in ComPany1.__members__:
    print(a)

#我們有時候可能會遇到列舉值轉換列舉tab的情況:#
from enum import Enum

class ComPany1(Enum):
    SALESMAN = 1
    ADMINISTRATIVE = 2
    LOGISTICS = 3
    MANAGEMENT = 4

print(ComPany1(1))

#除了Enum外還有IntEnum,Enum的列舉值可以是各種型別的資料,而IntEnum的列舉值則只能是數字#
from enum import IntEnum

class ComPany1(IntEnum):
    SALESMAN = 1
    ADMINISTRATIVE = 2
    LOGISTICS = 3
    MANAGEMENT = 4

#如果我們需要規定列舉值不能重複,則可以使用unique:#
from enum import IntEnum,unique

@unique
class ComPany1(IntEnum):
    SALESMAN = 1
    ADMINISTRATIVE = 2
    LOGISTICS = 3
    MANAGEMENT = 4
#如果我們將隨便兩個列舉值重複,則會報錯#

#另外列舉的設計模式是單例模式,所以,一般不會進行例項化#