|
| 1 | +## Goal |
| 2 | + |
| 3 | +The goal of this exercise is to teach the student how Enums ([Enumerations](https://en.wikipedia.org/wiki/Enumerated_type)) are implemented & used in Python. We will teach this through Pythons Enum class/type. |
| 4 | + |
| 5 | +## Learning objectives |
| 6 | + |
| 7 | +- What is an enumeration / enum is, and why they are needed/wanted in Python? |
| 8 | +- Understand the nomenclature (naming) used in reference to enums (e.g. enumeration/enum, enum members, enum member names, and enum member values) |
| 9 | +- Understand that enumeration members are functionally constants (and therefore should be formatted as such) |
| 10 | +- How Enums are different from other, more generic Python classes. |
| 11 | +- How to create various Enums |
| 12 | + - By using the class syntax by importing & subclassing Enum. |
| 13 | + - By using the Enum Functional API |
| 14 | + - By defining enum members with values that include non-int types (like str, tuple, float etc.) |
| 15 | + - Using the auto() function to automatically assign integer values to members when exact value types are unimportant/not needed. |
| 16 | + - Creating member aliases by assigning the same value to different names (two different names can have the same value; the second name defined becomes an alias of the first) |
| 17 | + - Using the class decorator @enum.unique to prevent member aliases and enforce that member values be unique. |
| 18 | +- How to use an enumeration and enumeration members in other functions/code |
| 19 | + - Enum members are iterable in member definition order, but iteration will not include aliases. |
| 20 | + - An ordered mapping of names to members can be retrieved via **members**.items() |
| 21 | + - enumeration members are compared by identity, using the is/is not keywords |
| 22 | + - Ordered comparison (<, >, <=, '>=) between enumeration valuesis not supported, and will throw aTypeError`. |
| 23 | + - Equality/inequality comparison is defined, and == and != can be used. |
| 24 | + - Comparisons between enumeration values and non-enumeration values will always return False |
| 25 | + |
| 26 | +## Out of scope |
| 27 | + |
| 28 | +- Flag enum subtype (perhaps better as an advanced exercise that includes bitwise operations) |
| 29 | +- IntEnum and IntFlag subtypes (since they break the semantic promises of an enum by being comparable to int) |
| 30 | +- mixins & multiple inheritance for the creation of Enums |
| 31 | +- using **new**() to customize the value of an enum member (better for an advanced/extended enum exercise) |
| 32 | +- omitting values |
| 33 | +- subclassing an "empty" pre-defined enum |
| 34 | +- customization of auto() |
| 35 | +- Pickling |
| 36 | + |
| 37 | +## Concepts |
| 38 | + |
| 39 | +- enumeration, enums |
| 40 | + |
| 41 | +## Prerequisites |
| 42 | + |
| 43 | +- `decorators, @` |
| 44 | +- `__init__()` |
| 45 | +- `classes, OOP` |
| 46 | +- `inheritance` |
| 47 | +- `iteration` |
| 48 | +- `iterables` |
| 49 | +- `dunder methods` |
| 50 | +- `comparisons` |
| 51 | +- `rich comparisons` |
| 52 | +- `class attributes` |
| 53 | +- `importing` |
| 54 | +- `aliasing` |
| 55 | +- `dicts, dict methods (specifically dict.items())` |
| 56 | +- `mapping types` |
| 57 | +- `immutable, immutability` |
| 58 | +- `class properties` |
| 59 | + |
| 60 | +## Resources |
| 61 | + |
| 62 | +- [Exercism v3 C# Enums concept exercise](https://github.com/exercism/v3/tree/master/languages/csharp/exercises/concept/enums) |
| 63 | +- [Python Docs: Enum](https://docs.python.org/3/library/enum.html) |
| 64 | +- [Enum - Mouse vs. Python](https://www.blog.pythonlibrary.org/2018/03/20/python-3-an-intro-to-enumerations/) |
| 65 | +- [Why you should use more enums in Python - Florian Dahlitz](https://florian-dahlitz.de/blog/why-you-should-use-more-enums-in-python) |
| 66 | +- [Python Docs: How are Enums Different?](https://docs.python.org/3/library/enum.html#how-are-enums-different) |
| 67 | +- [Python Docs: The Enum Functional API](https://docs.python.org/3/library/enum.html#functional-api) |
| 68 | +- [Stack Overflow: Python enum, when and where to use?](https://stackoverflow.com/questions/22586895/python-enum-when-and-where-to-use) |
| 69 | +- [PEP435: Adding an Enum Type to the Python Standard Library](https://www.python.org/dev/peps/pep-0435/) |
| 70 | +- [Using Enums and Django model Choices - Ben Cleary](https://medium.com/@bencleary/using-enums-as-django-model-choices-96c4cbb78b2e) |
0 commit comments