enum — 支持枚举

Added in version 3.4.

源代码: Lib/enum.py


枚举:

  • is a set of symbolic names (members) bound to unique values

  • can be iterated over to return its canonical (i.e. non-alias) members in definition order

  • 使用 call syntax to return members by value

  • 使用 index 句法以按名称返回成员

Enumerations are created either by using class syntax, or by using function-call syntax:

>>> from enum import Enum
>>> # class syntax
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
>>> # functional syntax
>>> Color = Enum('Color', [('RED', 1), ('GREEN', 2), ('BLUE', 3)])