Avast ye, this be a machine-translated text an’ may contain errors, aye!
Enums (enumerations) be a way to define a data type that can only hold a limited set o’ predefined values. This makes it easier to avoid errors by ensurin’ a variable can only have valid values.
Enums ain’t strictly used for validation, but they help us limit which values be valid for a variable, and thus can help prevent errors.
For example, if ye have a class that represents a ship, ye can use an Enum to define what colours the ship can be:
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.
Task 1 - Create an Enum
Create an Enum called EyeColor and implement it in the Person class from the previous module. EyeColor shall have the following values:
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.

