Python 简明教程

Python - Enums

Enums in Python

在 Python 中,术语 enumeration 引用向一组字符串分配固定常量值以便每个字符串都可通过与之绑定的值识别,的过程。包含在 enum 模块(Python 标准库的一部分)中的 Enum 类用作定义一组标识符(传统上以大写形式书写)的枚举的父类。

In Python, the term enumeration refers to the process of assigning fixed constant values to a set of strings so that each string can be identified by the value bound to it. The Enum class included in enum module (which is a part of Python’s standard library) is used as the parent class to define enumeration of a set of identifiers − conventionally written in upper case.

Example

在以下代码中,"subjects" 是枚举。它有不同的枚举成员,并且每个成员都是枚举类 subjects 的对象。这些成员有 name 和 value 属性。

In the below code, "subjects" is the enumeration. It has different enumeration members and each member is an object of the enumeration class subjects. These members have name and value attributes.

# importing enum
from enum import Enum

class subjects(Enum):
   ENGLISH = 1
   MATHS = 2
   SCIENCE = 3
   SANSKRIT = 4

obj = subjects.MATHS
print (type(obj))

它导致以下 output

It results in following output

<enum 'subjects'>

枚举类不允许出现两次相同成员,但可以为多个成员分配相同的值。为了确保每个成员都有一个与其绑定的唯一值,请使用 @unique 装饰器。

An enum class cannot have the same member appearing twice, however, more than one member may be assigned the same value. To ensure that each member has a unique value bound to it, use the @unique decorator.

Example

在此示例中,我们正在使用 @unique 装饰器来限制重复。

In this example, we are using the @unique decorator to restrict duplicacy.

from enum import Enum, unique

@unique
class subjects(Enum):
   ENGLISH = 1
   MATHS = 2
   GEOGRAPHY = 3
   SANSKRIT = 2

这将引发如下所示的异常 −

This will raise an exception as shown below −

   @unique
    ^^^^^^
   raise ValueError('duplicate values found in %r: %s' %
ValueError: duplicate values found in <enum 'subjects'>: SANSKRIT -> MATHS

Enum 类是一个可调用的类,因此可以利用其构造函数创建枚举。该构造函数接受两个参数,它们是枚举的名称和一个字符串,该字符串由枚举成员符号名称组成,这些名称之间以空格隔开。

The Enum class is a callable class, hence you can use its constructor to create an enumeration. This constructor accepts two arguments, which are the name of enumeration and a string consisting of enumeration member symbolic names separated by a whitespace.

Example

以下是一种定义枚举的替代方法 −

following is an alternative method of defining an enumeration −

from enum import Enum
subjects = Enum("subjects", "ENGLISH MATHS SCIENCE SANSKRIT")
print(subjects.ENGLISH)
print(subjects.MATHS)
print(subjects.SCIENCE)
print(subjects.SANSKRIT)

此代码将提供以下 output

This code will give the following output

subjects.ENGLISH
subjects.MATHS
subjects.SCIENCE
subjects.SANSKRIT

Accessing Modes in Enums

枚举类的成员可以通过两种模式访问 −

Members of an enum class can be accessed in two modes −

  1. Value − In this mode, value of the enum member is accessed using the "value" keyword followed by object of the enum class.

  2. Name − Similarly, we use the "name" keyword to access name of the enum member.

Example

以下示例说明如何访问枚举成员的值和名称。

The following example illustrates how to access value and name of the enum member.

from enum import Enum

class subjects(Enum):
   ENGLISH = "E"
   MATHS = "M"
   GEOGRAPHY = "G"
   SANSKRIT = "S"

obj = subjects.SANSKRIT
print(type(obj))
print(obj.name)
print(obj.value)

它将生成以下 output

It will produce the following output

<enum 'subjects'>
SANSKRIT
S

Iterating through Enums

可以按其在定义中出现的顺序遍历枚举成员,这要借助 for 循环。

You can iterate through the enum members in the order of their appearance in the definition, with the help of a for loop.

Example

以下示例说明如何使用 for 循环遍历枚举 −

The following example shows how to iterate through an enumeration using for loop −

from enum import Enum

class subjects(Enum):
   ENGLISH = "E"
   MATHS = "M"
   GEOGRAPHY = "G"
   SANSKRIT = "S"

for sub in subjects:
   print (sub.name, sub.value)

它将生成以下 output

It will produce the following output

ENGLISH E
MATHS M
GEOGRAPHY G
SANSKRIT S

我们知道,可以藉由分配给它的唯一值或通过其 name 属性访问枚举成员。因此,subjects("E") 和 subjects["ENGLISH"] 均返回 subjects.ENGLISH 成员。

We know that enum member can be accessed with the unique value assigned to it, or by its name attribute. Hence, subjects("E") as well as subjects["ENGLISH"] returns subjects.ENGLISH member.