Learn Python in 30 Days — Day 24: Intro to Classes & Objects
Day 24: Intro to Classes & Objects
Welcome to Day 24 of the Learn Python in 30 Days series!
Today we’re not just writing small programs we’re starting to structure code properly. And classes are the tool that lets you do that cleanly.
If you’re building games, tools, apps, or anything that has “things” in it, players, enemies, items, devices, then classes help represent those things in a neat, organised way.
Let’s break it down with simple, clear examples.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
What Are Classes and Objects?
A class is a blueprint.
An object is something created from that blueprint.
For example:
-
A Player class, contains data like name, health, score
-
A Player object, a specific player, like Matty, Bob, Alice
Here’s the simplest possible class:
This does nothing yet, but it shows the basic structure.
Creating a Simple Class
Let’s build a very basic class and create an object from it.
What happens here?
-
The class has a special method called
__init__(). -
This runs automatically when you create an object.
-
p1 = Player()triggers the print statement.
It’s a simple first step, but it shows how Python “builds” objects.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub simpleclass.py and run it yourself.
Storing Data Using __init__()
We almost never want empty objects.
We want them to store information such as names, scores, stats, anything.
Let’s add attributes.
Creating objects
Explanation
-
self.nameandself.healthare now saved inside the object. -
Each object gets its own set of values.
-
You can create as many players as you want without rewriting code.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub simpledata.py and run it yourself.
Adding Methods (Functions Inside Classes)
Methods allow objects to do things.
Let’s add an action for our player.
Using the method
Important Notes
-
Methods must include
selfas their first argument. -
selfis the object calling the method. -
Without
self, Python wouldn’t know which object’s data to change.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub addingmethods.py and run it yourself.
A Class With Multiple Methods
Let’s build something more realistic.
Using the class
Why this matters
Now your code is:
-
tidy
-
reusable
-
expandable
This is exactly the kind of structure you’ll use in the final project of the series (text adventure game) where you’ll build a Player and Room class — shown in your full 30-day roadmap .
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub multimethods.py and run it yourself.
A Full Working Example
This example uses everything we learned today.
Using it
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub example.py and run it yourself.
Next Up — Day 25 – Program Structure & Planning
Tomorrow, you’ll look into how to break big problems into smaller functions/files.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty





Comments
Post a Comment