Python中的枚举
Python中的枚举是通过使用名为“ enum ”的模块实现的。枚举是使用类创建的。枚举具有与之关联的名称和值。
枚举的属性:
1.枚举可以显示为字符串或repr。
2.可以使用type()检查枚举的类型。
3. “ name ”关键字用于显示枚举成员的名称。
Python3
# Python code to demonstrate enumerations
# importing enum for enumerations
import enum
# creating enumerations using class
class Animal(enum.Enum):
dog = 1
cat = 2
lion = 3
# printing enum member as string
print ("The string representation of enum member is : ",end="")
print (Animal.dog)
# printing enum member as repr
print ("The repr representation of enum member is : ",end="")
print (repr(Animal.dog))
# printing the type of enum member using type()
print ("The type of enum member is : ",end ="")
print (type(Animal.dog))
# printing name of enum member using "name" keyword
print ("The name of enum member is : ",end ="")
print (Animal.dog.name)
Python3
# Python code to demonstrate enumerations
# iterations and hashing
# importing enum for enumerations
import enum
# creating enumerations using class
class Animal(enum.Enum):
dog = 1
cat = 2
lion = 3
# printing all enum members using loop
print ("All the enum values are : ")
for Anim in (Animal):
print(Anim)
# Hashing enum member as dictionary
di = {}
di[Animal.dog] = 'bark'
di[Animal.lion] = 'roar'
# checking if enum values are hashed successfully
if di=={Animal.dog : 'bark',Animal.lion : 'roar'}:
print ("Enum is hashed")
else: print ("Enum is not hashed")
Python3
# Python code to demonstrate enumerations
# Access and comparison
# importing enum for enumerations
import enum
# creating enumerations using class
class Animal(enum.Enum):
dog = 1
cat = 2
lion = 3
# Accessing enum member using value
print ("The enum member associated with value 2 is : ",end="")
print (Animal(2))
# Accessing enum member using name
print ("The enum member associated with name lion is : ",end="")
print (Animal['lion'])
# Assigning enum member
mem = Animal.dog
# Displaying value
print ("The value associated with dog is : ",end="")
print (mem.value)
# Displaying name
print ("The name associated with dog is : ",end="")
print (mem.name)
# Comparison using "is"
if Animal.dog is Animal.cat:
print ("Dog and cat are same animals")
else : print ("Dog and cat are different animals")
# Comparison using "!="
if Animal.lion != Animal.cat:
print ("Lions and cat are different")
else : print ("Lions and cat are same")
输出 :
The string representation of enum member is : Animal.dog
The repr representation of enum member is :
The type of enum member is :
The name of enum member is : dog
4.枚举是可迭代的。它们可以使用循环进行迭代
5.枚举支持散列。枚举可用于字典或集合。
Python3
# Python code to demonstrate enumerations
# iterations and hashing
# importing enum for enumerations
import enum
# creating enumerations using class
class Animal(enum.Enum):
dog = 1
cat = 2
lion = 3
# printing all enum members using loop
print ("All the enum values are : ")
for Anim in (Animal):
print(Anim)
# Hashing enum member as dictionary
di = {}
di[Animal.dog] = 'bark'
di[Animal.lion] = 'roar'
# checking if enum values are hashed successfully
if di=={Animal.dog : 'bark',Animal.lion : 'roar'}:
print ("Enum is hashed")
else: print ("Enum is not hashed")
输出 :
All the enum values are :
Animal.dog
Animal.cat
Animal.lion
Enum is hashed
访问方式:枚举成员可以通过两种方式访问
1. 按值:- 在此方法中,传递枚举成员的值。
2. 按名称:- 在此方法中,传递枚举成员的名称。
也可以使用“ name ”或“ value ”关键字访问单独的值或名称。
比较:枚举支持两种类型的比较
1. 身份:- 这些使用关键字“ is ”和“ is not ”进行检查。
2. 相等:- 还支持“ == ”和“ != ”类型的相等比较。
Python3
# Python code to demonstrate enumerations
# Access and comparison
# importing enum for enumerations
import enum
# creating enumerations using class
class Animal(enum.Enum):
dog = 1
cat = 2
lion = 3
# Accessing enum member using value
print ("The enum member associated with value 2 is : ",end="")
print (Animal(2))
# Accessing enum member using name
print ("The enum member associated with name lion is : ",end="")
print (Animal['lion'])
# Assigning enum member
mem = Animal.dog
# Displaying value
print ("The value associated with dog is : ",end="")
print (mem.value)
# Displaying name
print ("The name associated with dog is : ",end="")
print (mem.name)
# Comparison using "is"
if Animal.dog is Animal.cat:
print ("Dog and cat are same animals")
else : print ("Dog and cat are different animals")
# Comparison using "!="
if Animal.lion != Animal.cat:
print ("Lions and cat are different")
else : print ("Lions and cat are same")
输出 :
The enum member associated with value 2 is: Animal.cat
The enum member associated with name lion is: Animal.lion
The value associated with dog is: 1
The name associated with dog is: dog
Dog and cat are different animals
Lions and cat are different