Avast ye, this be a machine-translated text an’ may contain errors, aye!
Enums (enumerations) be a way to define a type o’ data that can only hold a limited set o’ pre-defined values. This makes it easier to avoid mistakes by ensurin’ a variable can only carry valid treasures.
Enums ain’t strictly used for validation, but they help us limit which values be valid for a variable, and thus they can help prevent sinkin’ our code into errors.
For example, if ye have a class representin’ a ship, ye can use an Enum to define what colors the vessel can sport:
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):
# This be the make o' the car, aye!
self.make = make
# 'Tis the model, savvy?
self.model = model
# The year it were built, arr!
self.year = year
# And the color, shiver me timbers!
self.color = color
Why, Ye Ask?
Our IDE be givin’ us finer autocompletion when we be usin’ enums, an’ we be avoidin’ (makin’ it a hardship) to set invalid values. If we try to assign a colour that ain’t defined in CarColor, Python will be castin’ a blunder. This be makin’ the code more sturdy an’ easier to maintain. The fewer possible errors that can arise, the better quality we get in the code, aye.
Picture this, we have a function that handles payment methods: PayPal, VISA, MASTERCARD an’ BITCOIN. By usin’ an Enum for payment methods, we can be ensurin’ that the function only receives valid payment methods, an’ not "MASTERCARDD" or "CASH", which can lead to errors in the program.
Quest 1 - Forge an Enum
Forge an Enum named EyeColor and implement it within the Person class from the prevous module. EyeColor shall hold these treasures:
BLUEBROWNGREENHAZEL
Enums be much used in game development
Should ye be lookin’ in the documentation o’ various games, ye’ll often see ‘em usin’ Enums to define things like weapons, foes, levels, and the like. This be ‘cause it makes it easier to handle such things in the code, and it makes it easier to avoid errors.

