Avast ye, this be a machine-translated text an’ may contain errors, aye!
Enums
Enums (enumerations) be a way to define a data type that can on’y hold a limited set o’ pre-defined values. This makes it easier to avoid mistakes by ensurein’ a variable can on’y have valid values.
Strictly speakin’, enums ain’t used fer validation, but they help us limit which values be valid fer a variable, an’ thus they can contribute to preventin’ errors.
Fer instance, if ye’ve got a class representin’ a ship, ye could use an enum to define which colors the ship can wear:
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):
self.make = make
self.model = model
self.year = year
self.color = color
Why ‘Arr Matey?
Our ship’s blueprint gives us sharper auto-completer when we use enums, an’ it makes it hard (or nigh impossible) to set invalid values in the hold. If ye try t’ assign a color not defined in CarColor, Python will throw a fit over board. This makes our code more sturdy an’ easier t’ maintain. The fewer possible errors that can pop up, the better quality we get on our deck plans.
Picture this: we got a function dealin’ with payment methods: PayPal, VISA, MASTERCARD, an’ BITCOIN. By usin’ an Enum fer payment methods, we can ensure the function only accepts valid ways o’ payin’, an’ not "MASTERCARDD" or "CASH", which could lead t’ trouble in our riggin’.
Task 1 - Forge an Enum
Forge yerself an Enum named EyeColor and rig it into the Person class from the last deck o’ modules. This here EyeColor needs to carry these values:
BLUEBROWNGREENHAZEL
Be yer hullaballoo, me matey, fer enums be used in game development like a barnacle on a ship’s hull!
Should ye consult the docs o’ various games, ye’ll often spy that they use Enums to define things such as weapons, foes, levels and similar contraptions. ‘Tis because it makes it easier to handle such matters in code, an’ keeps errors away from our path more easily than a storm cloud!

