Python Glossary Русский

Python 3.6+enumterm

enum.IntFlag

flags

Bit flags that are also ints (combine and compare as numbers).

Syntax

class C(enum.IntFlag): ...

IntFlag defaults to boundary=KEEP: a value carrying a bit that no member defines is accepted and kept rather than rejected, so a corrupt mask stays silent until it bites. Members also compare and do arithmetic with ordinary ints, which makes accidental mixing with unrelated numbers easy. Use IntFlag where the value crosses out of your program (system calls, re flags, packed bit fields); for logic that stays inside, Flag is cleaner.

Examples

import enum
class Mode(enum.IntFlag):
    A = 1
    B = 2
print((Mode.A | Mode.B) == 3)   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy