Python中的Enum类

Python中的Enum类

什么是Enum

在Python中,枚举是一组命名的值,用于表示有限集合的可能结果。例如,假设我们正在编写一个需要表示一周中的天的程序,我们可以定义一个包含七个命名值的枚举:星期一、星期二、星期三、星期四、星期五、星期六和星期日。

枚举允许您以比仅使用字符串或整数更有意义的方式处理这些值。例如,您可以使用==运算符比较两个星期几,并且它会按预期运行:

Enum的特点

  • 用户可以使用 type() 来检查枚举的类型
  • 通过使用关键字’ name ‘ ,用户可以显示枚举的名称。
  • 枚举是对象的可评估字符串表示,称为 repr()

示例:创建枚举的Enum类

import enum
# we will use enum class for creating enumerations
class Weekdays(enum.Enum):
   Sunday = 1
   Monday = 2
   Tuesday = 3
   Wednesday = 4
   Thursday = 5
   Friday = 6
   Saturday = 7
# we will print the enum member as the string
print (" The member of Enum class as the string is : ",end = " ")
print (Weekdays.Monday)

# we will print the enum member as a repr object
print (" The member of Enum class as a repr is : ",end = " ")
print (repr(Weekdays.Sunday))

# now we will check the type of enum member
print (" The type of the member of Enum class is : ",end = " ")
print (type(Weekdays.Saturday))

# we will print name of enum member
print (" The name of the member of Enum class is : ",end = " ")
print (Weekdays.Friday.name)

输出:

The member of Enum class as the string is : Weekdays.Monday
The member of Enum class as a repr is : 
The type of the member of Enum class is : 
The name of the member of Enum class is : Friday

示例2:如何将Enum打印为可迭代列表

用户可以将Enum类作为可迭代列表进行打印。

在下面的示例中,我们将使用for循环来打印Enum类的所有成员。

代码

import enum
# we will user enum class for creating enumerations
class Weekdays(enum.Enum):
   Sunday = 1
   Monday = 2
   Tuesday = 3
   Wednesday = 4
   Thursday = 5
   Friday = 6
   Saturday = 7
# now we will print all enum members by using for loop
print (" The member of Enum class are : ")
for weekday in (Weekdays):
   print(weekday)

输出:

The member of Enum class are : 
Weekdays.Sunday
Weekdays.Monday
Weekdays.Tuesday
Weekdays.Wednesday
Weekdays.Thursday
Weekdays.Friday
Weekdays.Saturday

示例3:如何对枚举类进行哈希

枚举类的成员被称为枚举,也是可哈希的。因此,这些成员可以用于集合和字典。

代码

import enum
# we will use enum class for creating enumerations
class Days(enum.Enum):
   Sunday = 1
   Monday = 2
# we will Hash for creating a dictionary
Daytype = {}
Daytype[Days.Sunday] = 'Sun God'
Daytype[Days.Monday] = 'Mon God'
# now we will Check if the hashing is successful
if Daytype =={Days.Sunday:'Sun God',Days.Monday:'Mon God'}:
 print (" Enum class is hashed ")
else: print (" Enum class is not hashed ")

输出:

Enum class is hashed

示例4:如何访问枚举成员

用户可以通过使用成员项的值或名称来访问Enum类的成员。枚举的名称用作索引。

代码

import enum
# we will use enum class for creating enumerations
class Days(enum.Enum):
  Sunday = 1
  Monday = 2
  Tuesday = 3
  Wednesday = 4
  Thursday = 5 
  Friday = 6
  Saturday = 7
print('The member of Enum class accessed by name: ')
print (Days['Monday'])
print('The member of Enum class accessed by name: ')
print (Days['Friday'])
print('The member of Enum class accessed by Value: ')
print (Days(1))
print('The member of Enum class accessed by Value: ')
print (Days(5)) 

输出:

The member of Enum class accessed by name: 
Days.Monday
The member of Enum class accessed by name: 
Days.Friday
The member of Enum class accessed by Value: 
Days.Sunday
The member of Enum class accessed by Value: 
Days.Thursday

示例5:如何比较枚举

要比较枚举,我们使用以下方法:

  1. 使用” == “和” != “运算符
  2. 使用” is “运算符
  3. 使用” in “运算符

代码

import enum
# we will use enum class for creating enumerations
class Days(enum.Enum):
  Sunday = 1
  Monday = 2
  Tuesday = 1
  Wednesday = 4
  Thursday = 5 
  Friday = 4
  Saturday = 7
if Days.Sunday == Days.Tuesday:
   print('Match')
else: print ('Do not Match')
if Days.Monday != Days.Tuesday:
    print('Do not Match')
else: print ('Match')
if Days.Wednesday == Days.Friday:
   print('Match')
else: 
print ('Do not Match')
if Days.Thursday != Days.Friday:
    print('Do not Match')
else: 
print ('Match')

输出:

Match
Do not Match
Match
Do not Match

结论

在本文中,我们探讨了Python中的枚举类,它允许我们对枚举进行定义和操作。通过使用枚举,我们可以使代码更加一致和少出错误,并且提供额外的类型安全性。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程