Python Glossary Русский

Python 3.11+enumterm

enum.FlagBoundary

flags

The strategy for out-of-range flag bits: STRICT/CONFORM/EJECT/KEEP (3.11+).

Syntax

enum.FlagBoundary

It answers what should happen when a value carries a bit that no member defines. The defaults differ, and that is the usual trap: Flag uses STRICT, so an undefined bit raises ValueError, while IntFlag uses KEEP and quietly carries the stray bit along. You choose a strategy with the boundary keyword in the class header.

Examples

import enum
print(enum.FlagBoundary.STRICT.name)   # → STRICT
print(list(enum.FlagBoundary.__members__))   # → ['STRICT', 'CONFORM', 'EJECT', 'KEEP']
import enum
class Perm(enum.Flag): R = 1; W = 2
print(Perm(5))   # → ValueError
import enum
class Soft(enum.Flag, boundary=enum.CONFORM): R = 1; W = 2
print(Soft(5).value)   # → 1

See also

Open in the full glossarydocs.python.orgReport an inaccuracy