4.3. Enum Auto
auto()
Automatically generated value
Care must be taken if mixing auto() with manually specified values.
If used, the
Enum
machinery will call anEnum._generate_next_value_()
to get an appropriate value.Enum._generate_next_value_()
can be overridden to customize the values used by auto.
4.3.1. IntEnum
Automatically generated integer values
By default: last value plus one
>>> from enum import IntEnum, auto
>>>
>>>
>>> class Color(IntEnum):
... RED = auto()
... GREEN = auto()
... BLUE = auto()
>>>
>>> for color in Color:
... print(f'color: {color.name}, value: {color.value}')
...
color: RED, value: 1
color: GREEN, value: 2
color: BLUE, value: 3
4.3.2. StrEnum
Automatically generated string values
By default: lower-cased version of the member's name
>>> from enum import StrEnum, auto
>>>
>>>
>>> class Color(StrEnum):
... RED = auto()
... GREEN = auto()
... BLUE = auto()
>>>
>>> for color in Color:
... print(f'color: {color.name}, value: {color.value}')
...
color: RED, value: red
color: GREEN, value: green
color: BLUE, value: blue
4.3.3. Flag
Automatically generated power-of-two values
By default: first power-of-two greater than the last value
>>> from enum import Flag, auto
>>>
>>>
>>> class Color(Flag):
... RED = auto()
... GREEN = auto()
... BLUE = auto()
>>>
>>> for color in Color:
... print(f'color: {color.name}, value: {color.value}')
...
color: RED, value: 1
color: GREEN, value: 2
color: BLUE, value: 4
4.3.4. Assignments
# %% About
# - Name: Enum Auto 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` 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, \
'Variable `Color` has an invalid length; expected: `3`.'
>>> 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
# %% About
# - Name: Enum Auto 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` and `auto()`
# 3. 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` 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 IntEnum in Color.mro(), \
'Class `Color` has an invalid type; expected: `IntEnum`.'
>>> assert len(Color) == 3, \
'Variable `Color` has an invalid length; expected: `3`.'
>>> 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, auto
# %% Types
Color: type[IntEnum]
# %% Data
# %% Result