Quest 19: Classes - Building Your Own Blueprints

Create Custom Objects with Data and Behaviour

๐Ÿ—๏ธ QUEST 19 | Difficulty: Intermediate | Time: 10 minutes

๐Ÿ“Š Complexity Level: Intermediate โญโญ

Builds on fundamental concepts from earlier quests. Best for students who have completed Quests 1-6 or have some basic programming experience. This introduces object-oriented programming - a major paradigm in software development.

๐Ÿ’ป Interactive Options:

  • ๐Ÿ““ Open in JupyterLite - Full Jupyter environment in your browser
  • โ–ถ๏ธ Run code directly below - All code cells on this page are editable and runnable
  • ๐Ÿ“ฅ Download Notebook - For use in local Jupyter or Google Colab

๐Ÿ“– Introduction: The Blueprint Idea

Imagine youโ€™re designing a video game with many different characters โ€” warriors, mages, rogues. Each character has the same kinds of information: a name, a health level, an attack power. Each character can do the same kinds of things: take damage, heal, attack.

Would you write separate code for every single character? No way! Youโ€™d write one blueprint (called a class) and then stamp out as many characters (called objects or instances) as you need, each with their own values.

Thatโ€™s exactly what Python classes are โ€” blueprints for creating objects.

๐Ÿฐ Story Time: Think of a class as the mould used to make sword replicas in a castle armoury. Every sword from that mould has the same shape, but you can stamp a different name on each one, paint it a different colour, and nick the blade differently from battles. The mould is the class; each sword is an object (an instance of the class).

๐Ÿ’ก Explanation: Anatomy of a Class

class Character:               # Class definition โ€” capitalise the name!
    
    def __init__(self, name, health):   # Constructor โ€” runs when you create an object
        self.name   = name              # Instance attribute
        self.health = health            # Instance attribute
    
    def greet(self):                    # Method (a function inside a class)
        print(f"Hi, I'm {self.name}!")

# Create objects (instances) from the class
hero   = Character("Luna", 100)
goblin = Character("Grak", 40)

hero.greet()    # Hi, I'm Luna!
goblin.greet()  # Hi, I'm Grak!

๐ŸŽฏ Key Terms

Term Meaning Example
class The blueprint class Character:
object / instance One thing made from the blueprint hero = Character("Luna", 100)
attribute Data stored on an object hero.name, hero.health
method A function that belongs to a class hero.greet()
__init__ Special method called when creating an object def __init__(self, name):
self The object itself (Python passes it automatically) First parameter in every method

๐ŸŽฎ Activity 1: Creating Your First Class

Letโ€™s build a simple Hero class step by step:

๐ŸŽฎ Activity 2: Methods That Interact with Other Objects

One of the coolest things about classes is that objects can interact with each other:

๐ŸŽฎ Activity 3: Class Attributes vs Instance Attributes

So far all our attributes belong to individual objects. But you can also attach data to the class itself โ€” shared by everyone:

๐ŸŽฎ Activity 4: Special Methods ( __str__ and __repr__ )

Python has many dunder (double-underscore) methods that let your class work with built-in Python behaviour like print() and len():

๐ŸŽฎ Activity 5: Building a Mini RPG Inventory System

Letโ€™s put everything together and build a small but complete class system:

๐Ÿงฉ Challenge: Design a Bank Account Class

Write a BankAccount class with: - Attributes: owner, balance - Method deposit(amount) โ€” adds money, prints confirmation - Method withdraw(amount) โ€” removes money if balance allows, otherwise prints an error - Method __str__ โ€” returns a nice summary string

โœ… Challenge Solution

๐ŸŽ“ Summary: Classes at a Glance

class MyClass:               # Blueprint definition
    shared = "class attr"    # Class attribute (shared)

    def __init__(self, x):   # Constructor
        self.x = x           # Instance attribute (unique)

    def do_thing(self):      # Method
        return self.x * 2

    def __str__(self):       # Friendly string representation
        return f"MyClass({self.x})"

obj = MyClass(5)             # Create an instance
obj.do_thing()               # Call a method โ†’ 10
print(obj)                   # Uses __str__  โ†’ MyClass(5)

๐ŸŒ Why Classes Matter

Classes are the foundation of Object-Oriented Programming (OOP) โ€” one of the most widely used approaches in software development. Once you understand classes, youโ€™ll find them everywhere: - Pythonโ€™s built-in types (list, dict, str) are all classes - Web frameworks, game engines, data science libraries โ€” all built with classes - You now have the skills to read and write professional Python code!

Congratulations โ€” youโ€™ve built your own custom blueprints! The world of Python has opened up significantly. What will you create? ๐Ÿ—๏ธ๐ŸŽ‰