This doth be a machine-wrought text which may contain errors!
Enums (enumerations) be a fashion to define a data type which may hold but a limited set of values pre-defined. This doth make it the more facile to eschew errors, for a variable may possess only values lawful.
Enums are not, in sooth, employed for validation, yet they do assist us in constraining which values be valid for a variable, and thus may contribute to the prevention of errors.
For example, shouldst thou have a class which doth represent an automobile, thou mayest employ an Enum to define which hues the vehicle may bear:
from enum import Enum
class CarColor(Enum):
RED = "red"
BLUE = "blue"
GREEN = "green"
BLACK = "black"
WHITE = "white"
class Car:
def __init__(self, make: str, model: str, year: int, color: CarColor):
# Dette er en bil
# 'Tis a carriage, good sir.
self.make = make
self.model = model
self.year = year
self.color = color
Wherefore?
Our IDE doth grant better autocompletion when we employ enums, and we avoid (making it difficult) to set invalid values. Should we attempt to assign a colour which is not defined in CarColor, Python will cast an error. This doth render the code more robust and easier to maintain. The fewer possible errors that may arise, the better quality we obtain in the code.
Imagine, prithee, that we have a function which doth handle payment methods: PayPal, VISA, MASTERCARD and BITCOIN. By employing an Enum for payment methods, we may assure that the function doth receive only valid payment methods, and not "MASTERCARDD" or "CASH", which may lead to errors in the programme.
Task the First – An Enumeration to Forge
Forge ye an Enumeration hight EyeColor, and do implement the same within the Person class from the module past. EyeColor shall bear these values:
BLUEBROWNGREENHAZEL
Enum doth oft find use in the crafting of games
Shouldst thou seek wisdom within the scrolls of divers games, thou shalt oft perceive them employing Enums to define such things as arms, foes, levels, and the like. This doth render the handling of such matters within the code more facile, and doth ease the avoidance of errors.

