Enumeration is a common way to store values that can only represent a few states. Each symbolic name is bound to a specific value, usually numeric, that represents the states the enumeration can have.
Enumerations are very common in other programming languages, but until recently, Python didn't have any explicit support for enumerations.
Typically, enumerations are implemented by mapping symbolic names to numeric values; this is allowed in Python through enum.IntEnum
:
>>> from enum import IntEnum
>>>
>>> class RequestType(IntEnum):
... POST = 1
... GET = 2
>>>
>>> request_type = RequestType.POST
>>> print(request_type)
RequestType.POST
IntEnum
is an integer, apart from the fact that all possible values are created when the class is defined. IntEnum
inherits from int
, so its values are real integers.
During the RequestType
definition, all the possible values for enum
are declared within the class body and the values are verified against duplicates by the metaclass.
Also, enum
provides support for a special value, auto
, which means just put in a value, I don't care. As you usually only care whether it's POST
or GET
, you usually don't care whether POST
is 1
or 2
.
Last but not least, enumerations cannot be subclassed if they define at least one possible value.
IntEnum
values behave like int
in most cases, which is usually convenient, but they can cause problems if the developer doesn't pay attention to the type.
For example, a function might unexpectedly perform the wrong thing if another enumeration or an integer value is provided, instead of the proper enumeration value:
>>> def do_request(kind):
... if kind == RequestType.POST:
... print('POST')
... else:
... print('OTHER')
As an example, invoking do_request
with RequestType.POST
or 1
will do exactly the same thing:
>>> do_request(RequestType.POST)
POST
>>> do_request(1)
POST
When we want to avoid treating our enumerations as numbers, we can use enum.Enum
, which provides enumerated values that are not considered plain numbers:
>>> from enum import Enum
>>>
>>> class RequestType(Enum):
... POST = 1
... GET = 2
>>>
>>> do_request(RequestType.POST)
POST
>>> do_request(1)
OTHER
So generally, if you need a simple set of enumerated values or possible states that rely on enum
, Enum
is safer, but if you need a set of numeric values that rely on enum
, IntEnum
will ensure that they behave like numbers.