


This article brings you a detailed explanation (code example) of enumeration types in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Python’s native types do not include enumeration types. To provide a better solution, Python added the enum standard library in version 3.4 via PEP 435.
The enumeration type can be regarded as a label or a collection of constants, usually used to represent certain limited collections, such as week, month, status, etc. What do we do when there is no specifically provided enumeration type? Generally, it is implemented through a dictionary or class:
Color = { 'RED' : 1, 'GREEN': 2, 'BLUE' : 3, } class Color: RED = 1 GREEN = 2 BLUE = 3
This way of implementing enumeration is of course no problem if it is used carefully. After all, it is a compromise. s solution. The danger is that it can be modified.
Using Enum
A better way is to use the Enum
type provided by the standard library. The official library is trustworthy. Versions before 3.4 can also download supported libraries through pip install enum
. Simple example:
from enum import Enum class Color(Enum): red = 1 green = 2 blue = 3
Enumeration members have values (repeatable by default), and enumeration members have friendly string representations:
>>> print(Color.red) Color.red >>> print(repr(Color.red)) <color.red:> >>> type(Color.red) <enum> >>> isinstance(Color.green, Color) True</enum></color.red:>
Enumeration types are not instantiable and cannot be changed.
Define enumeration
When defining an enumeration, the member name is not allowed to be repeated
class Color(Enum): red = 1 green = 2 red = 3 # TypeError: Attempted to reuse key: 'red'
The member value is allowed to be the same, the second member The name is regarded as an alias of the first member
class Color(Enum): red = 1 green = 2 blue = 1 print(Color.red) # Color.red print(Color.blue) # Color.red print(Color.red is Color.blue)# True print(Color(1)) # Color.red 在通过值获取枚举成员时,只能获取到第一个成员
If you cannot define the same member value, you can decorate it with unique
from enum import Enum, unique @unique class Color(Enum): red = 1 green = 2 blue = 1 # ValueError: duplicate values found in <enum>: blue -> red</enum>
Enumeration value
You can get members by member name or member value:
print(Color['red']) # Color.red 通过成员名来获取成员 print(Color(1)) # Color.red 通过成员值来获取成员
Each member has a name attribute and a value attribute:
member = Color.red print(member.name) # red print(member.value) # 1
supports iteration through members in the defined order. If there are members with duplicate values, only the first duplicate member is obtained:
for color in Color: print(color)
Special attributes__members__
is a An ordered dictionary mapping names to members, through which traversal can also be done:
for color in Color.__members__.items(): print(color) # ('red', <color.red:>)</color.red:>
Enumeration comparison
Members of an enumeration can be passed through is
Identity comparison or equality comparison through ==
:
Color.red is Color.red Color.red is not Color.blue Color.blue == Color.red Color.blue != Color.red
Enumeration members cannot perform size comparison:
Color.red <p><strong> Extended enumeration IntEnum</strong></p><p>IntEnum is an extension of Enum. Different types of integer enumerations can also be compared with each other: </p><pre class="brush:php;toolbar:false">from enum import IntEnum class Shape(IntEnum): circle = 1 square = 2 class Request(IntEnum): post = 1 get = 2 print(Shape.circle == 1) # True print(Shape.circle = Request.post) # True
Summary
enum module function is very clear , its usage is also simple, and its implementation method is worth learning. If you have the opportunity, you can take a look at its source code.
The above is the detailed content of Detailed explanation of enumeration types in Python (code examples). For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)
