4.3. Enum StrEnum

  • List of finite choices

  • Enumerations with str values

  • StrEnum

4.3.1. StrEnum

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

4.3.2. Auto

>>> from enum import StrEnum, auto
>>>
>>>
>>> class Color(StrEnum):
...     RED = auto()
...     GREEN = auto()
...     BLUE = auto()
>>>
>>>
>>> for color in Color:
...     print(color.name, color.value)
...
RED red
GREEN green
BLUE blue

4.3.3. Use Case - 1

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

4.3.4. Use Case - 2

  • Dead or Alive

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

4.3.5. Use Case - 3

>>> class Ordinal(StrEnum):
...     NORTH = 'N'
...     SOUTH = 'S'
...     EAST = 'E'
...     WEST = 'W'

4.3.6. Use Case - 4

>>> class Mood(StrEnum):
...     SAD = 'sad'
...     HAPPY = 'happy'

4.3.7. Use Case - 5

  • Issue Status

>>> class IssueStatus(StrEnum):
...     TODO = 'todo'
...     IN_PROGRESS = 'in-progress'
...     IN_REVIEW = 'in-review'
...     IN_TEST = 'in-test'
...     DONE = 'done'
...     REJECTED = 'rejected'

4.3.8. Use Case - 6

  • HTML Colors

>>> class Color(StrEnum):
...     AQUA = '#00FFFF'
...     BLACK = '#000000'
...     BLUE = '#0000FF'
...     FUCHSIA = '#FF00FF'
...     GRAY = '#808080'
...     GREEN = '#008000'
...     LIME = '#00FF00'
...     MAROON = '#800000'
...     NAVY = '#000080'
...     OLIVE = '#808000'
...     PINK = '#FF1A8C'
...     PURPLE = '#800080'
...     RED = '#FF0000'
...     SILVER = '#C0C0C0'
...     TEAL = '#008080'
...     WHITE = '#FFFFFF'
...     YELLOW = '#FFFF00'

4.3.9. Use Case - 7

>>> from dataclasses import dataclass
>>>
>>>
>>> class Role(StrEnum):
...     PRODUCT_OWNER = 'PO'
...     SCRUM_MASTER = 'SM'
...     TEAM_MEMBER = 'TM'
>>>
>>>
>>> @dataclass
... class User:
...     firstname: str
...     lastname: str
...     role: Role
>>>
>>>
>>> alice = User('Alice', 'Apricot', role='not-existing')
>>> alice = User('Alice', 'Apricot', role=Role.TEAM_MEMBER)

4.3.10. Use Case - 8

>>>
... from django.db import models
...
... class HttpMethod(models.TextChoices):
...     GET = 'GET', _('GET')
...     POST = 'POST', _('POST')
...     PATCH = 'PATCH', _('PATCH')
...     PUT = 'PUT', _('PUT')
...     HEAD = 'HEAD', _('HEAD')
...     DELETE = 'DELETE', _('DELETE')
...     OPTIONS = 'OPTIONS', _('OPTIONS')
...     TRACE = 'TRACE', _('TRACE')
...     CONNECT = 'CONNECT', _('CONNECT')
...
...
... class Stage(models.TextChoices):
...     PRODUCTION = 'production', _('Production')
...     TEST = 'test', _('Test')

4.3.11. Assignments

# %% About
# - Name: Enum StrEnum Define
# - 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 `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 StrEnum Auto
# - 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 `Color`:
# - name: RED, value: 'red'
# - name: GREEN, value: 'green'
# - name: BLUE, value: 'blue'
# 2. Use `StrEnum` and `auto()`
# 3. 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` i `auto()`
# 3. 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, auto

# %% Types
Color: type[StrEnum]

# %% Data

# %% Result