Harry Potter and The Object-Oriented Programming Paradigm (Harry Potter OOP Series #1)
Year 1 at Hogwarts School of Codecraft and Algorithmancy • The Mindset
"Mr and Mrs Dursley, of number four, Privet Drive, were proud to say that they believed the classic way of computer programming was the one and only path to mastery, thank you very much. They were the last people you’d expect to be involved in anything strange or mysterious such as the perplexing world of object-oriented programming, because they just didn't hold with such nonsense.
“Within the perfectly disciplined walls of their home, mentioning such peculiar concepts was strictly off-limits. Little did they know that the seemingly bewildering universe of innovative and efficient programming practices would not be so easily warded off and would soon unexpectedly intertwine with their seemingly normal lives."
These are the opening paragraphs in an alternative Harry Potter series. In my version, Harry is about to start learning at Hogwarts School of Codecraft and Algorithmancy. The school teaches young coding wizards the concepts of classes and object-oriented programming in Python.
Introduction To The Series
This is the first in a series of seven articles, each linked to a year at Hogwarts School of Codecraft and Algorithmancy. I won't cover every possible aspect of classes and object-oriented programming. However, I'll cover a fair amount of detail. As you'd expect from seven years in senior school, the material will get more advanced as you progress from one year to the next.
Year 1 (this article) is designed to ensure the new students settle into a new school and learn the mindset needed for object-oriented programming. The students will find out that they've been using objects already in their previous coding experience. They'll revisit what they've learnt in primary school, but this time, they'll look at it using a new mindset—the object-oriented programming mindset.
Year 2: Students will start defining classes and learning about data attributes
Year 3: It's time to define methods in the classes
Year 4: Students build on their knowledge and learn about the interaction between classes
Year 5: Inheritance. The students are ready for this now they're older
Year 6: Students learn about special methods. You may know these as dunder methods
Year 7: It's the final year in which students learn about class methods and static methods
And then, they're ready to graduate to the real world.
Distribution Note: I'll only email some of the articles in this series to reduce the pressure on your inboxes. The articles I won’t email will still be available online once published, of course! I'll link to the previous posts in all articles, just in case you miss any. I'll also announce when I publish new articles in the series on Notes and other platforms.
Fresh-Faced First Years Arrive at Hogwarts
The first-year students have arrived at Hogwarts School of Codecraft and Algorithmancy. They got sorted into their houses—we'll talk about houses and the Sorting Hat in later years. And now they're ready to start lessons.
Their first class was called What is Programming? They were confused going in as they were sure they knew very well what programming was. They've been programming since they started primary school, after all.
The professor strode in, cloak flapping with every stride. He flicked his wand, and bits of chalk started screeching on the blackboard:
What is Programming?
It's storing data and doing stuff with the data
In every program you've written, you stored data in variables. You also performed actions using functions.
Is this different in object-oriented programming?
No. And Yes.
Bundling The "Storing" With The "Doing"
In object-oriented programming—let's go with OOP from now on—the "storing data" and the "doing stuff with the data" bits of the professor's blackboard statement are bundled together into a single unit.
This single unit is an object. And yes, that's the same object as the first 'O' in OOP. An object contains the data and the tools needed to do stuff with the data.
Here’s an object:
Let's break this line of code down:
You create an object. This object is a list
You also create three strings. These are also objects. But we'll focus on the list object here
You create the name
hogwarts_houses
. This is the label for the list object
We often say that hogwarts_houses
is an object. And that's perfectly fine. Although if I put the 'pedant hat' on, I'd say that hogwarts_houses
is the name which refers to the list object. But let's not get bogged down on detail. It's Year 1, after all!
The object stores data. In this case, it stores three strings. (Pedant here, again: It stores references to the strings, not the strings themselves. But let's just let this pass.)
However, the object also "carries with it" the actions it can perform on the data it stores. Here's an example:
The object, which is a list, has access to .append()
. This performs an action on the object. It adds another item to the list.
Every list has access to .append()
. Therefore, whenever you create an object which is a list, the function* .append()
comes bundled with it, ready to be used when needed. This function will act on the data in the object.
*I've called .append()
a function. And it is a function. However, we'll see in Year 2 that we call these methods when they're attached to an object.
The Students' Next Class Is About Class
You can create many objects that are all lists. They'll contain different items of data, but they're all lists. And you can create many strings—different strings, but they're all strings. You get the gist.
Objects of the same type are part of the same class. You can confirm that hogwarts_houses
is a member of the list class:
The built-in type()
shows you the object's class is a list
. This class already exists in Python—you didn't have to create it yourself. In Year 2, the students at Hogwarts School of Codecraft and Algorithmancy will learn how to define their own classes.
All objects of the same class share the same characteristics and have access to the same actions, such as .append()
for list objects. A class is a "template" or a "blueprint" that can be used to create objects that are similar to each other, follow the same pattern, and have access to the same functions (methods).
In Python, everything is an object. Therefore, everything is a member of a class. You've been using classes and objects since you wrote your first line of Python code, even if you didn't know it.
The OOP Mindset
So, a class brings "storing data" and "doing stuff with the data" under one roof. So what?
This is not just a technical difference. It affects the philosophy underpinning how you design and plan your programs. We often call OOP a programming paradigm—a different mindset to solving problems using a computer program.
Here's one way of getting into this new mindset. So far, you've been adapting your thinking to fit into the way a computer program works. Well, you'll still need to do this. However, OOP allows you to see the problem from a human's point of view. In fact, it encourages you to do so. Rather than bending your thinking to fit a computer, you now have a paradigm that's better suited to the human's way of seeing things.
You'll see this concept in action in Years 2 and 3 of your studies at Hogwarts School of Codecraft and Algorithmancy. We ask ourselves what "objects" make sense for a human being thinking about the problem. And we'll create classes to define those "objects". For example, we'll create a Wizard
class and a Wand
class. We'll give them characterstics which are unique to each class and enable them to perform actions that a member of the class should be able to perform. Then, we can create as many wizards and wands as we need.
More on this next 'year'.
Things To Know As You Learn About The OOP Paradigm
The end of Year 1 is near. But there are still a few more lessons left before the holidays.
All the professors seem to be drilling the following point into the students: Take your time with object-oriented programming. It's unlikely it will "fully make sense" the first time you learn about it. Or the second. You need to give it time to "brew"!
And in the end-of-year banquet, the headmaster warned the first years with the following words, which you'll also find if you're brave enough to read stuff on the world-wide-web:
There are three types of people in the programming world:
Those who love OOP
Those who hate OOP
Those who are pragmatists
Author's personal note: The third group is the one with the cool kids in it
Don't get swayed by those in groups 1 and 2. Especially those in group 2. I've mentioned earlier that OOP is one of several paradigms you can use when programming. It's a style of programming. Some people like it more than others.
But this leads me to the third group of people: the pragmatists. OOP is a tool that has advantages and disadvantages. As you learn how to use this tool, you'll also learn when to use it. The first time you learn about OOP, you'll be tempted to use it in every project you work on. You'll start defining classes for everything! But, with time and experience, you'll learn when you'll benefit from defining classes—and when not to use them.
Terminology Corner
Class: A class is a "template" or "blueprint" for creating objects with the same characteristics and behaviour. Objects of the same class are not identical, but they are similar
Object: An object is an individual unit created from a class, which contains data and has actions associated with it
You've made it to the end of Year 1 at Hogwarts School of Codecraft and Algorithmancy. Enjoy the holidays. We'll meet again at the start of Year 2, when you'll learn about defining your own classes and how to store information in data attributes.
Next article in this series: Year 2 • Let The Real Magic Begin • Creating Classes in Python
Code in this article uses Python 3.11
Stop Stack
Recently published articles on The Stack:
Iterable: Python's Stepping Stones. What makes an iterable iterable? Part 1 of the Data Structure Categories Series
Why Do 5 + "5" and "5" + 5 Give Different Errors in Python? • Do You Know The Whole Story? If
__radd__()
is not part of your answer, read on…
You may notice that I've published the first article in two different series. I'll be publishing these concurrently. I'm still at the phase where I'm making the transition from Twitter being my main outlet for new content to Substack. So, these are ideas that I've presented on Twitter in the past and I'm now writing out in full as series of articles.
With series such as these, I won't email every article in the series. Some will be just published on The Stack without sending them out by email.
By the way, if you want a more traditional* chapter about object-oriented programming, you can read Chapter 7 | Object-Oriented Programming in The Python Coding Book. (*I don't really do 'traditional', but it's a more conventional format than this series!)
The first cohort of The Python Coding Programme starts this week. Live sessions with very small cohorts over 3 weeks, with 90 minutes live on Zoom every day (4 days a week). Each cohort only has 4 participants and there's active mentoring throughout with a private forum for the cohort to continue discussions. Here's some more information about The Python Coding Programme for Beginners.
Really enjoy the work you do. Thank you.
Now that's what I call a creative twist to technical writing! 😎