Python教程-Python中的枚举类
什么是枚举?
在Python中,枚举是一组具有名称的值,用来表示有限的一组可能结果。例如,假设我们正在编写一个需要表示一周中的日期的程序,我们可以定义一个包含七个命名值的枚举:Monday、Tuesday、Wednesday、Thursday、Friday、Saturday和Sunday。
枚举允许您以比仅使用字符串或整数更有意义的方式使用这些值。例如,您可以使用等于运算符比较两个星期中的日期,并且它将按预期工作:
枚举的特点:
- 用户可以使用 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:如何对枚举类进行哈希
Enum类的成员称为Enumeration,并且可哈希。因此,这些成员可以用于集合和字典。
代码
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成员
用户可以通过值或成员项的名称访问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:如何比较枚举
要比较枚举,我们可以使用以下方法:
- 使用 "==" 和 "!=" 运算符
- 使用 "is" 运算符
- 使用 "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中的枚举类,它允许我们在代码中定义和操作枚举。通过使用枚举,我们可以使代码更加可读,减少错误,并提供额外的类型安全性。