On this page
enum.IntEnum
Prev: enum.global_str | Up: enum | Next: enum.IntFlag
Rel: enum.auto, enum.Enum, enum.EnumCheck, enum.EnumDict
Ref: docs.python.org — enum.IntEnum
Tags: #public #python3 #stdlib #enum
Import
import enum
from enum import IntEnum
Signature & Overview
enum.IntEnum(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)
Enum where members are also (and must be) ints
Methods & Attributes
IntEnum.as_integer_ratio(self, /)— Return a pair of integers, whose ratio is equal to the original int.IntEnum.bit_count(self, /)— Number of ones in the binary representation of the absolute value of self.IntEnum.bit_length(self, /)— Number of bits necessary to represent self in binary.IntEnum.conjugate(self, /)— Returns self, the complex conjugate of any int.IntEnum.from_bytes(bytes, byteorder='big', *, signed=False)— Return the integer represented by the given array of bytes.IntEnum.is_integer(self, /)— Returns True. Exists for duck type compatibility with float.is_integer.IntEnum.to_bytes(self, /, length=1, byteorder='big', *, signed=False)— Return an array of bytes representing an integer.
Usage
from enum import IntEnum
class HttpStatus(IntEnum):
OK = 200
NOT_FOUND = 404
assert HttpStatus.OK == 200
assert HttpStatus.OK + 1 == 201
Gotchas
- Subclass of
int: Interoperates directly with APIs expecting integers and JSON serializers, at the cost of allowing cross-enum comparisons (HttpStatus.OK == 200). - Arithmetic drops the enum type:
HttpStatus.OK + 1returns a plainint, not anHttpStatus.
REPL Inspect
from enum import IntEnum
help(IntEnum)
Linked from
Related notes
- tuts/python3/stdlib_module/enum/global_str Linked here; links to this page; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/IntFlag Linked here; links to this page; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/auto Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/Enum Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/EnumCheck Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/EnumDict Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/enum/EnumMeta shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/EnumType shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/Flag shared tags: #enum, #stdlib