Python Glossary Русский

Python 3.4+enumterm

enum.IntEnum

enum kinds

An enumeration whose members are also ints (comparable to and usable as numbers).

Syntax

class C(enum.IntEnum): ...

The convenience costs you type safety: a member equals a plain number and equals any member of a different IntEnum with the same value, so two unrelated enums compare equal without a peep. Since Python 3.11 str() and f-strings render an IntEnum member as the bare number instead of Color.RED, which silently rewrites log lines when old code moves to a newer version. Reach for IntEnum to interoperate with code and APIs that expect ints; for logic of your own, plain Enum is safer.

Examples

import enum
class Color(enum.IntEnum):
    RED = 1
    GREEN = 2
print(Color.RED == 1)        # → True
print(int(Color.GREEN) + 1)  # → 3

See also

Open in the full glossarydocs.python.orgReport an inaccuracy