PyDocs

Nav

Data Types

Enum

enum

class (source)
  • Enum(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)
Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

- attribute access:

  >>> Color.RED
  <Color.RED: 1>

- value lookup:

  >>> Color(1)
  <Color.RED: 1>

- name lookup:

  >>> Color['RED']
  <Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3

>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own
attributes -- see the documentation for details.
Changed in 3.12
  • 3.12: Added Dataclass support.
Members of Enum

_

__dir__
Returns public methods and other interesting attributes.
__format__
Default object formatter.
__init__
Initialize self.
__init_subclass__
This method is called when a class is subclassed.
__new__
Create and return a new object.
__repr__
Return repr(self).
__str__
Return str(self).
_add_alias_
Adds a new name as an alias to an existing member:
_add_value_alias_
Adds a new value as an alias to an existing member:
_generate_next_value_
Generate the next value when not given.
_ignore_
_ignore_ is only used during creation and is removed from the enumeration once creation is complete.
_missing_
A classmethod for looking up values not found in cls.
_name_
Name of the member.
_order_
No longer used, kept for backward compatibility.
_value_
Value of the member, can be set in __new__().

n

name
The name of the Enum member.

v

value
The value of the Enum member.
1 Example
>>> 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)])
from the Python docs
See Also
No see-alsos for enum/Enum
0 Notes
No notes for Enum