5.1. Enum About — Python
List of finite choices
Enumerations
5.1.1. SetUp
>>> from enum import Enum
5.1.2. Syntax
>>> class Color(Enum): ... RED = 'r' ... GREEN = 'g' ... BLUE = 'b'
5.1.3. Get Name, Value
>>> mycolor = Color('r') >>> >>> mycolor <Color.RED: 'r'> >>> >>> mycolor.name 'RED' >>> >>> mycolor.value 'r'
5.1.4. Comparison
>>> mycolor = Color('r') >>> >>> mycolor is Color.RED True >>> >>> mycolor is Color.GREEN False
5.1.5. Iteration
>>> for color in Color: ... print(color) Color.RED Color.GREEN Color.BLUE
5.1.6. Methods
>>> class Color(Enum): ... RED = 'r' ... GREEN = 'g' ... BLUE = 'b' ... ... @classmethod ... def get_favourite(cls): ... return cls.RED
>>> Color.get_favourite() <Color.RED: 'r'>
5.1.7. Enum vs. Dict
Enum:
>>> class Color(Enum): ... RED = 'r' ... GREEN = 'g' ... BLUE = 'b' ... >>> >>> Color.RED <Color.RED: 'r'> >>> >>> Color('r') <Color.RED: 'r'>
Dict:
>>> color = { ... 'RED': '#FF0000', ... 'GREEN': '#00FF00', ... 'BLUE': '#0000FF', ... } >>> >>> color['RED'] '#FF0000' >>> >>> color['#FF0000'] Traceback (most recent call last): KeyError: '#FF0000' >>> >>> tmp = {v:k for k,v in color.items()} >>> tmp['#FF0000'] 'RED'
5.1.8. Use Case - 1
HTML Colors
>>> class Color(Enum): ... 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'
5.1.9. Use Case - 2
>>> from enum import Enum >>> >>> >>> class Color(Enum): ... RED = '#FF0000' ... GREEN = '#00FF00' ... BLUE = '#0000FF' >>> >>> >>> def plot(x, y, color): ... if not isinstance(color, Color): ... available_colors = ', '.join(c.name for c in Color) ... raise TypeError(f'color must be a Color: {available_colors}') ... print('Plotting point...')
>>> plot(1, 2, color='red') Traceback (most recent call last): TypeError: color must be a Color: RED, GREEN, BLUE
>>> plot(1, 2, color=Color.RED) Plotting point...
5.1.10. Assignments
# %% About # - Name: Enum About Color # - 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: '#FF0000' # - name: GREEN, value: '#00FF00' # - name: BLUE, value: '#0000FF' # 2. Use `Enum` # 3. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj enum `Color`: # - nazwa: RED, wartość: '#FF0000' # - nazwa: GREEN, wartość: '#00FF00' # - nazwa: BLUE, wartość: '#0000FF' # 2. Użyj `Enum` # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> Color.RED.value # '#FF0000' # # >>> Color.GREEN.value # '#00FF00' # # >>> Color.BLUE.value # '#0000FF' # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert Enum in Color.mro(), \ 'Class `Color` 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 == '#FF0000' >>> assert Color.GREEN.value == '#00FF00' >>> assert Color.BLUE.value == '#0000FF' """ # %% 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 Enum # %% Types Color: type[Enum] # %% Data # %% Result
# %% About # - Name: Enum About Lookup # - Difficulty: easy # - Lines: 2 # - 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 variable `result` with an enum object for color `#FF0000` # 2. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj zmienną `result` z obiektem enum dla koloru `#FF0000` # 2. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # <Color.RED: '#FF0000'> # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert Enum in Color.mro(), \ 'Class `Color` 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 == '#FF0000' >>> assert Color.GREEN.value == '#00FF00' >>> assert Color.BLUE.value == '#0000FF' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is Color, \ 'Variable `result` has an invalid type; expected: `Color`.' >>> result <Color.RED: '#FF0000'> """ # %% 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 Enum # %% Types result: Enum # %% Data class Color(Enum): RED = '#FF0000' GREEN = '#00FF00' BLUE = '#0000FF' # %% Result result = ...
# %% About # - Name: Enum About Is # - Difficulty: easy # - Lines: 1 # - 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. Check if `DATA` is `Color.RED` # 2. Define variable `result` with the result # 3. Run doctests - all must succeed # %% Polish # 1. Sprawdź czy `DATA` jest `Color.RED` # 2. Zdefiniuj zmienną `result` z wynikiem # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # True # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert Enum in Color.mro(), \ 'Class `Color` 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 == '#FF0000' >>> assert Color.GREEN.value == '#00FF00' >>> assert Color.BLUE.value == '#0000FF' >>> assert isinstance(result, bool), \ 'Variable `result` has invalid type, must be a bool' >>> result True """ # %% 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 Enum # %% Types result: bool # %% Data class Color(Enum): RED = '#FF0000' GREEN = '#00FF00' BLUE = '#0000FF' DATA = Color('#FF0000') # %% Result result = ...
# %% About # - Name: Enum About Name and Value # - Difficulty: easy # - Lines: 2 # - 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 variable `result1` with a name of the color in `DATA` # 2. Define variable `result2` with a value of the color in `DATA` # 3. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj zmienną `result1` z nazwą koloru w `DATA` # 2. Zdefiniuj zmienną `result2` z wartością koloru w `DATA` # 3. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result1 # 'RED' # # >>> result2 # '#FF0000' # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert Enum in Color.mro(), \ 'Class `Color` 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 == '#FF0000' >>> assert Color.GREEN.value == '#00FF00' >>> assert Color.BLUE.value == '#0000FF' >>> assert 'result1' in globals(), \ 'Variable `result1` is not defined; assign result of your program to it.' >>> assert result1 is not Ellipsis, \ 'Variable `result1` has an invalid value; assign result of your program to it.' >>> assert type(result1) is str, \ 'Variable `result1` has an invalid type; expected: `str`.' >>> result1 'RED' >>> assert 'result2' in globals(), \ 'Variable `result2` is not defined; assign result of your program to it.' >>> assert result2 is not Ellipsis, \ 'Variable `result2` has an invalid value; assign result of your program to it.' >>> assert type(result2) is str, \ 'Variable `result2` has an invalid type; expected: `str`.' >>> result2 '#FF0000' """ # %% 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 Enum # %% Types result1: str result2: str # %% Data class Color(Enum): RED = '#FF0000' GREEN = '#00FF00' BLUE = '#0000FF' DATA = Color('#FF0000') # %% Result result1 = ... result2 = ...
# %% About # - Name: Enum About List # - Difficulty: easy # - Lines: 2 # - 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 variable `result` with a list of all names in `Color` # 2. Run doctests - all must succeed # %% Polish # 1. Zdefiniuj zmienną `result` z listą wszystkich nazw w `Color` # 2. Uruchom doctesty - wszystkie muszą się powieść # %% Expected # >>> result # [<Color.RED: '#FF0000'>, <Color.GREEN: '#00FF00'>, <Color.BLUE: '#0000FF'>] # %% Doctests """ >>> import sys; sys.tracebacklimit = 0 >>> assert sys.version_info >= (3, 9), \ 'Python has an is invalid version; expected: `3.9` or newer.' >>> assert Enum in Color.mro(), \ 'Class `Color` 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 == '#FF0000' >>> assert Color.GREEN.value == '#00FF00' >>> assert Color.BLUE.value == '#0000FF' >>> assert 'result' in globals(), \ 'Variable `result` is not defined; assign result of your program to it.' >>> assert result is not Ellipsis, \ 'Variable `result` has an invalid value; assign result of your program to it.' >>> assert type(result) is list, \ 'Variable `result` has an invalid type; expected: `list`.' >>> result [<Color.RED: '#FF0000'>, <Color.GREEN: '#00FF00'>, <Color.BLUE: '#0000FF'>] """ # %% 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 Enum # %% Types result: list[Enum] # %% Data class Color(Enum): RED = '#FF0000' GREEN = '#00FF00' BLUE = '#0000FF' # %% Result result = ...