ID EN
Enum

Flag

Python 3.11

Flag members support the bitwise operators & (AND), | (OR), ^ (XOR), and ~ (INVERT); the results of those operators are members of the enumeration.

Syntax

PYTHON
class enum.Flag

Examples

Example 1
PYTHON
>>> from enum import Flag, auto
>>> class Color(Flag):
...     RED = auto()
...     GREEN = auto()
...     BLUE = auto()
>>> purple = Color.RED | Color.BLUE
>>> white = Color.RED | Color.GREEN | Color.BLUE
>>> Color.GREEN in purple
False
>>> Color.GREEN in white
True
>>> purple in white
True
>>> white in purple
False
Example 2
PYTHON
>>> list(Color.RED)
[<Color.RED: 1>]
>>> list(purple)
[<Color.RED: 1>, <Color.BLUE: 4>]
Example 3
PYTHON
>>> len(Color.GREEN)
1
>>> len(white)
3
Example 4
PYTHON
>>> bool(Color.GREEN)
True
>>> bool(white)
True
>>> black = Color(0)
>>> bool(black)
False
Example 5
PYTHON
>>> Color.RED | Color.GREEN
<Color.RED|GREEN: 3>

See Also

hex() oct() auto Flag