5.2. Enum IntEnum — Python
EnumStrEnumIntEnumFlag
5.2.1. Enum
>>> from enum import Enum >>> >>> >>> class Color(Enum): ... RED = 1 ... GREEN = '#00FF00' ... BLUE = (0, 0, 255)
5.2.2. StrEnum
>>> from enum import StrEnum >>> >>> >>> class Color(StrEnum): ... RED = 'red' ... GREEN = 'green' ... BLUE = 'blue'
5.2.3. IntEnum
>>> from enum import IntEnum >>> >>> >>> class Color(IntEnum): ... RED = 1 ... GREEN = 2 ... BLUE = 3
5.2.4. Flag
FlagValues needs to be power-of-two's (1, 2, 4, 8, 16, ...)
Cannot have duplicated names
>>> from enum import Flag >>> >>> class Color(Flag): ... RED = 1 ... GREEN = 2 ... BLUE = 4 >>> >>> >>> mycolor = Color(3) >>> mycolor <Color.RED|GREEN: 3>
5.2.5. Case Study
Recap:
$ ls -l README.txt -rw-r--r-- 1 alice staff 4.0K 2000-01-01 00:00 README.txt
user =
rw-= 0b110 = 6group =
r--= 0b100 = 4others =
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>
5.2.6. Use Case - 1
>>> from enum import StrEnum >>> >>> >>> class Color(StrEnum): ... RED = '#FF0000' ... GREEN = '#00FF00' ... BLUE = '#0000FF'
5.2.7. Use Case - 2
>>> from enum import StrEnum >>> >>> >>> class Direction(StrEnum): ... NORTH = 'N' ... SOUTH = 'S' ... EAST = 'E' ... WEST = 'W'
5.2.8. Use Case - 3
>>> from enum import StrEnum >>> >>> >>> class Status(StrEnum): ... ALIVE = 'alive' ... DEAD = 'dead'
5.2.9. Use Case - 4
>>> from enum import IntEnum >>> >>> >>> class HTTPStatus(IntEnum): ... OK = 200 ... CREATED = 201 ... BAD_REQUEST = 400 ... NOT_FOUND = 404 ... INTERNAL_ERROR = 500
5.2.10. 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ść # %% 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 # %% 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ść # %% 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 `IntEnum` has an invalid type; expected: `Enum`.' >>> 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 # %% 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 `Color`: # - name: RED, value: 1 # - name: GREEN, value: 2 # - name: BLUE, value: 4 # 2. Use `Flag` # 3. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj enum `Color`: # - nazwa: RED, wartość: 1 # - nazwa: GREEN, wartość: 2 # - nazwa: BLUE, wartość: 4 # 2. Użyj `Flag` # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> Color.RED.value # 1 # # >>> Color.GREEN.value # 2 # # >>> Color.BLUE.value # 4 # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert Flag in Color.mro(), \ 'Color must be an Flag' >>> 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 == 4 """ # %% 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 Color: type[Flag] # %% Data # %% Result