Python Glossary Русский

Python 3.6+enumterm

enum.Flag

flags

A bit-flag enumeration whose members combine with | & ^ ~.

Syntax

class C(enum.Flag): ...

Member values must be single bits: auto() inside a Flag hands out 1, 2, 4 and so on, while a hand-written 3 creates a composite alias rather than a new flag and won't show up when you iterate the class. Unlike IntFlag, a Flag member is not an int, which is the point: it cannot be silently compared with or added to unrelated numbers. The empty combination (value 0) is falsy, so a plain truth test answers "is any flag set at all".

Examples

import enum
class Perm(enum.Flag):
    R = 1
    W = 2
    X = 4
print((Perm.R | Perm.W).value)      # → 3
print(Perm.R in (Perm.R | Perm.W))  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy