您的位置:

Python 中的枚举类

枚举是 Python 中用于开发枚举的类。枚举是绑定到常量和唯一值的一组符号成员或名称。枚举的成员可以通过使用这些符号名来等同。枚举可以自己在它们上面重复。

枚举类的特征是:

  • 用户可以使用 type()方法检查枚举的类型。
  • 通过使用“名称”关键字,用户可以显示枚举的名称。
  • 枚举是称为 repr()的对象的可计算字符串表示形式。

示例:


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 :  <enum>The name of the member of Enum class is :  Friday</enum> 

如何将枚举打印为可列表

用户可以将枚举类打印为可列表。

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

示例:


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

如何哈希枚举类

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

在下面的例子中,我们将展示用户如何哈希枚举类。

示例:


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

如何访问枚举成员

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

在下面的示例中,我们将展示用户如何通过名称访问值,其中我们使用了枚举的名称作为索引。

示例:


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

如何比较枚举

用户可以简单地通过使用比较运算符来比较枚举。

例如:


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 的枚举类及其特性。我们还展示了用户如何将枚举类打印为可迭代列表。用户如何哈希和访问枚举类的成员。