5.3. Enum Auto — Python
auto()Automatically generated value
Care must be taken if mixing auto() with manually specified values.
If used, the
Enummachinery 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.
5.3.1. 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() # 'red' ... GREEN = auto() # 'green' ... BLUE = auto() # 'blue' >>> >>> for color in Color: ... print(f'color: {color.name}, value: {color.value}') ... color: RED, value: red color: GREEN, value: green color: BLUE, value: blue
5.3.2. IntEnum
Automatically generated integer values
By default: last value plus one
>>> from enum import IntEnum, auto >>> >>> >>> class Color(IntEnum): ... RED = auto() # 1 ... GREEN = auto() # 2 ... BLUE = auto() # 3 >>> >>> for color in Color: ... print(f'color: {color.name}, value: {color.value}') ... color: RED, value: 1 color: GREEN, value: 2 color: BLUE, value: 3
5.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() # 1 (0b001) ... GREEN = auto() # 2 (0b010) ... BLUE = auto() # 4 (0b100) >>> >>> >>> for color in Color: ... print(f'color: {color.name}, value: {color.value}') ... color: RED, value: 1 color: GREEN, value: 2 color: BLUE, value: 4
5.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ść # %% Expected # >>> Color.RED.value # 'red' # # >>> Color.GREEN.value # 'green' # # >>> Color.BLUE.value # 'blue' # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> 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'), \ 'Object `Color` has an invalid attribute; expected: to have an attribute `RED`.' >>> assert hasattr(Color, 'GREEN'), \ 'Object `Color` has an invalid attribute; expected: to have an attribute `GREEN`.' >>> assert hasattr(Color, 'BLUE'), \ 'Object `Color` has an invalid attribute; expected: to have an attribute `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ść # %% Expected # >>> Color.RED.value # 1 # # >>> Color.GREEN.value # 2 # # >>> Color.BLUE.value # 3 # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> 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'), \ 'Object `Color` has an invalid attribute; expected: to have an attribute `RED`.' >>> assert hasattr(Color, 'GREEN'), \ 'Object `Color` has an invalid attribute; expected: to have an attribute `GREEN`.' >>> assert hasattr(Color, 'BLUE'), \ 'Object `Color` has an invalid attribute; expected: to have an attribute `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