August 2018
Intermediate to advanced
366 pages
10h 14m
English
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: ...