Home >Backend Development >Python Tutorial >Python enumeration class definition and function (example analysis)
In the following article, let’s learn about what is the enumeration class in python. Understand what python enumeration class is, and what role python enumeration class can play in python programming.
Definition of enumeration
First, to define an enumeration, import the enum module.
The enumeration definition uses the class keyword and inherits the Enum class.
Note:
When defining an enumeration, member names are not allowed to be repeated
By default, different member values are allowed to be the same. However, for two members with the same value, the name of the second member is regarded as an alias of the first member
If there are members with the same value in the enumeration, when obtaining the enumeration member by value, only Get the first member
If you want to restrict the definition of an enumeration, you cannot define members with the same value. You can use the decorator @unique [to import the unique module]
When we need to define constants, one way is to use uppercase variables to define them with integers, such as month:
JAN = 1 FEB = 2 MAR = 3 ... NOV = 11 DEC = 12
The advantage is that it is simple, but the disadvantage is that the type is int and it is still a variable.
A better approach is to define a class type for such an enumeration type, and then each constant is a unique instance of the class. Python provides the Enum class to implement this function:
from enum import Enum Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
In this way, we obtain the Month type enumeration class. We can directly use Month.Jan to reference a constant, or to enumerate all its members:
for name, member in Month.__members__.items(): print(name, '=>', member, ',', member.value)
The value attribute is an int constant automatically assigned to the member, counting from 1 by default.
If you need more precise control over the enumeration type, you can derive a custom class from Enum:
from enum import Enum, unique @unique class Weekday(Enum): Sun = 0 # Sun的value被设定为0 Mon = 1 Tue = 2 Wed = 3 Thu = 4 Fri = 5 Sat = 6
@The unique decorator can help us check to ensure that there are no duplicate values.
There are several ways to access these enumeration types:
>>> day1 = Weekday.Mon >>> print(day1) Weekday.Mon >>> print(Weekday.Tue) Weekday.Tue >>> print(Weekday['Tue']) Weekday.Tue >>> print(Weekday.Tue.value) 2 >>> print(day1 == Weekday.Mon) True >>> print(day1 == Weekday.Tue) False >>> print(Weekday(1)) Weekday.Mon >>> print(day1 == Weekday(1)) True >>> Weekday(7) Traceback (most recent call last): ... ValueError: 7 is not a valid Weekday >>> for name, member in Weekday.__members__.items(): ... print(name, '=>', member) ... Sun => Weekday.Sun Mon => Weekday.Mon Tue => Weekday.Tue Wed => Weekday.Wed Thu => Weekday.Thu Fri => Weekday.Fri Sat => Weekday.Sat
It can be seen that the enumeration constant can be referenced by the member name, or the enumeration constant can be obtained directly based on the value of the value.
The above is all the content described in this article. This article mainly introduces the relevant knowledge of python enumeration class. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.
For more related knowledge, please visit the Python tutorial column on the php Chinese website.
The above is the detailed content of Python enumeration class definition and function (example analysis). For more information, please follow other related articles on the PHP Chinese website!