4.2. Enum IntEnum

  • StrEnum

  • IntEnum

  • Flag

4.2.1. StrEnum

>>> from enum import StrEnum
>>>
>>>
>>> class Color(StrEnum):
...     RED = 'red'
...     GREEN = 'green'
...     BLUE = 'blue'

4.2.2. IntEnum

>>> from enum import IntEnum
>>>
>>>
>>> class Color(IntEnum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3

4.2.3. Flag

  • Flag

  • IntFlag

Flags have an expanded view of aliasing: to be canonical, the value of a flag needs to be a power-of-two value, and not a duplicate name. So, in addition to the Enum definition of alias, a flag with no value (a.k.a. 0) or with more than one power-of-two value (e.g. 3) is considered an alias.

Recap:

$ ls -l README.txt
-rw-r--r-- 1 alice staff 4.0K 2000-01-01 00:00 README.txt
  • user = rw- = 0b110 = 6

  • group = r-- = 0b100 = 4

  • others = r-- = 0b100 = 4

SetUp:

>>> from enum import Flag

Definition:

>>> class Permission(Flag):
...     READ = 0b100
...     WRITE = 0b010
...     EXECUTE = 0b001

Usage:

>>> Permission(1)
<Permission.EXECUTE: 1>
>>>
>>> Permission(2)
<Permission.WRITE: 2>
>>>
>>> Permission(3)
<Permission.WRITE|EXECUTE: 3>
>>>
>>> Permission(4)
<Permission.READ: 4>
>>>
>>> Permission(5)
<Permission.READ|EXECUTE: 5>
>>>
>>> Permission(6)
<Permission.READ|WRITE: 6>
>>>
>>> Permission(7)
<Permission.READ|WRITE|EXECUTE: 7>

Example:

>>> permission = Permission.READ | Permission.WRITE
>>> permission
<Permission.READ|WRITE: 6>

4.2.4. Use Case - 1

>>> from enum import StrEnum
>>>
>>>
>>> class Color(StrEnum):
...     RED = '#FF0000'
...     GREEN = '#00FF00'
...     BLUE = '#0000FF'

4.2.5. Use Case - 2

>>> from enum import StrEnum
>>>
>>>
>>> class Direction(StrEnum):
...     NORTH = 'N'
...     SOUTH = 'S'
...     EAST = 'E'
...     WEST = 'W'

4.2.6. Use Case - 3

>>> from enum import StrEnum
>>>
>>>
>>> class Status(StrEnum):
...     ALIVE = 'alive'
...     DEAD = 'dead'

4.2.7. Use Case - 4

>>> from enum import IntEnum
>>>
>>>
>>> class HTTPStatus(IntEnum):
...     OK = 200
...     CREATED = 201
...     BAD_REQUEST = 400
...     NOT_FOUND = 404
...     INTERNAL_ERROR = 500

4.2.8. Assignments

# %% About
# - Name: Enum Types StrEnum
# - Difficulty: easy
# - Lines: 4
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define enum `Color`:
# - name: RED, value: 'red'
# - name: GREEN, value: 'green'
# - name: BLUE, value: 'blue'
# 2. Use `StrEnum`
# 3. Do not use `auto()`
# 4. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj enum `Color`:
# - nazwa: RED, wartość: 'red'
# - nazwa: GREEN, wartość: 'green'
# - nazwa: BLUE, wartość: 'blue'
# 2. Użyj `StrEnum`
# 3. Nie używaj `auto()`
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert StrEnum in Color.mro(), \
'Color must be an StrEnum'

>>> assert len(Color) == 3, \
'Color must have 3 elements'

>>> assert hasattr(Color, 'RED')
>>> assert hasattr(Color, 'GREEN')
>>> assert hasattr(Color, 'BLUE')

>>> assert Color.RED.value == 'red'

>>> assert Color.GREEN.value == 'green'

>>> assert Color.BLUE.value == 'blue'
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
from enum import StrEnum

# %% Types
Color: type[StrEnum]

# %% Data

# %% Result

# %% About
# - Name: Enum Types IntEnum
# - Difficulty: easy
# - Lines: 4
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define enum `Color`:
# - name: RED, value: 1
# - name: GREEN, value: 2
# - name: BLUE, value: 3
# 2. Use `IntEnum`
# 3. Do not use `auto()`
# 4. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj enum `Color`:
# - nazwa: RED, wartość: 1
# - nazwa: GREEN, wartość: 2
# - nazwa: BLUE, wartość: 3
# 2. Użyj `IntEnum`
# 3. Nie używaj `auto()`
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert IntEnum in Color.mro(), \
'Color must be an IntEnum'

>>> assert len(Color) == 3, \
'Color must have 3 elements'

>>> assert hasattr(Color, 'RED')
>>> assert hasattr(Color, 'GREEN')
>>> assert hasattr(Color, 'BLUE')

>>> assert Color.RED.value == 1
>>> assert Color.GREEN.value == 2
>>> assert Color.BLUE.value == 3
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
from enum import IntEnum

# %% Types
Color: type[IntEnum]

# %% Data

# %% Result

# %% About
# - Name: Enum Types Flag
# - Difficulty: easy
# - Lines: 4
# - Minutes: 3

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 1. Define enum `Permission`:
# - name: READ, value: 0b100
# - name: WRITE, value: 0b010
# - name: EXECUTE, value: 0b001
# 2. Use `Flag`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj enum `Permission`:
# - nazwa: READ, wartość: 0b100
# - nazwa: WRITE, wartość: 0b010
# - nazwa: EXECUTE, wartość: 0b001
# 2. Użyj `Flag`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert Flag in Permission.mro(), \
'Permission must be an Flag'

>>> assert len(Permission) == 3, \
'Permission must have 3 elements'

>>> assert hasattr(Permission, 'READ')
>>> assert hasattr(Permission, 'WRITE')
>>> assert hasattr(Permission, 'EXECUTE')

>>> assert Permission.READ.value == 0b100
>>> assert Permission.WRITE.value == 0b010
>>> assert Permission.EXECUTE.value == 0b001
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
from enum import Flag

# %% Types
Permission: type[Flag]

# %% Data

# %% Result