Home  >  Article  >  Backend Development  >  How to use enumeration types in python

How to use enumeration types in python

silencement
silencementOriginal
2019-05-24 13:24:298828browse

How to use enumeration types in python: [from enum import Enum class xxx(Enum)]. How to define an enumeration: First, you need to import the enum module; then define the enumeration through the class keyword and inherit the Enum class.

How to use enumeration types in python

Definition of enumeration:

First, to define an enumeration, import the enum module.

The enumeration is defined using the class keyword and inherits the Enum class.

The enumeration type enum is a more important data type. It is a data type rather than a data structure. We usually declare a set of commonly used constants as an enumeration type to facilitate subsequent use. When a variable has several possible values, we define it as an enumeration type. How is it implemented in Python?

We declare the enumeration type of a month:

How to use enumeration types in python

How to use enumeration types in python

##First import the enum module, and then declare the enumeration type name and its possible values. There is another way we define an Enum subclass to define an enumeration class.

How to use enumeration types in python

#@unique This decorator helps us check whether there are duplicate values. Retrieving values ​​from enumeration types is also diverse.

You can also see from the last one that there is a difference between defining an enumeration class and defining an ordinary class. As mentioned at the beginning, the enumeration type is a set of constants. We just combine a set of possible values ​​for future use. Put the value constant in one place and retrieve the value as needed.

The enumeration type enum is a more important data type. It is a data type rather than a data structure. We usually declare a set of commonly used constants as an enumeration type to facilitate subsequent use. When a variable has several possible values, we define it as an enumeration type.

The above is the detailed content of How to use enumeration types in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is a 404 pageNext article:What is a 404 page