35 Why Do We Need Classes? A Tale of Adventure and Code
Imagine you’re creating a game about Parzival’s quest for the Holy Grail. You need to keep track of many characters, each with their own attributes like health, strength, and inventory. Let’s see how we might try this without classes:
# Create our hero
= "Parzival"
hero_name = 100
hero_health = 15
hero_strength = "Excalibur"
hero_weapon = ["health potion", "magic scroll", "shield"]
hero_inventory
# Create a companion
= "Galahad"
companion_name = 95
companion_health = 14
companion_strength = "Blessed Sword"
companion_weapon = ["healing herbs", "holy water"]
companion_inventory
# Create another companion
= "Lancelot"
companion2_name = 98
companion2_health = 16
companion2_strength = "Arondight"
companion2_weapon = ["magic ring", "armor polish"] companion2_inventory
This works, but it’s already getting messy. What if we need to add more characters? Or what if we want to give a healing potion to Galahad? We’d need something like:
def heal_character(name, health, amount):
if name == "Parzival":
+= amount
hero_health elif name == "Galahad":
+= amount
companion_health elif name == "Lancelot":
+= amount
companion2_health
def attack_enemy(attacker_name, attacker_strength, attacker_weapon,
enemy_name, enemy_health):# This would be even messier!
pass
The problems are piling up:
- We have to create new variables for every single attribute of every character
- We have to remember which variables go with which character
- Our functions need to handle each character separately
- If we want to add a new attribute (like “magic power”), we need to add it everywhere
- There’s no easy way to create new characters without copying and pasting code
This is where classes come to our rescue! With classes, we can create a template for what a character should look like:
class Character:
def __init__(self, name, health, strength, weapon):
self.name = name
self.health = health
self.strength = strength
self.weapon = weapon
self.inventory = []
# Now creating characters is easy!
= Character("Parzival", 100, 15, "Excalibur")
parzival = Character("Galahad", 95, 14, "Blessed Sword")
galahad = Character("Lancelot", 98, 16, "Arondight") lancelot
Think of a class like a cookie cutter - it defines the shape and structure of what we want to create. Each character we create from this class is like a cookie made from that cutter. They all have the same basic structure (name, health, strength, weapon), but each can have its own unique values.
Classes give us:
- A way to keep all related data together (each character’s attributes stay with them)
- A blueprint for creating new objects of the same type
- A cleaner, more organized way to write our code
- The ability to add new features to all characters by updating the class
Just as the knights of the Round Table shared certain characteristics (bravery, honor, combat skills) but were each unique individuals, objects created from a class share a common structure but can have their own unique values.
In the lessons that follow, we’ll learn how to create these magical blueprints called classes, and breathe life into them by creating objects. We’ll discover how to give our objects abilities (methods), create different types of objects that share characteristics (inheritance), and much more.
Get ready to level up your coding skills - it’s time to master the art of object-oriented programming!