Python 中的枚举类
枚举是 Python 中用于开发枚举的类。枚举是一组绑定到常量和唯一值的符号成员或名称。可以使用这些符号名称来标识枚举成员。上面的枚举可以重复。
枚举类的特点是:
- 用户可以使用 type() 方法检查枚举类型。
- 通过使用“name”关键字,用户可以显示枚举的名称。
- 枚举是称为 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的枚举类及其特性。我们还展示了用户如何将枚举类打印为可迭代列表。用户如何散列和访问枚举类成员。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
上一篇:Python 返回语句 下一篇:Python 中的析构函数
code前端网