Python OOP For Beginners - Classes & Objects

As you mature in programming, the first thing that comes to your mind when OOP is mentioned, is classes. Here is the why, what and when of it.

Introduction

This is the second article in the Python OOP article series. If you haven’t read the previous article, I highly recommend checking it out first.

Do you remember? We faced a problem in the previous article. We had to pass the same initial values about the dog again and again each time we called a function (a behavior) like run or speak.

We only had two behaviors there. Imagine having many behaviors like eating, sleeping, running, walking, etc. We'd have to at least pass the dog’s name as an argument to those functions repeatedly.

In this article, we'll focus on solving that problem.

Defining A Class

We're going to define a class called Dog to solve this issue.

What Is A Class?

Search "What is a class?" anywhere, and you’ll see the same answer almost everywhere: a blueprint for creating objects. And honestly—I can’t disagree with that.

For example, if you have a class called Person, it’s the blueprint for each person object you create. If you have a Dog class, it’s the blueprint for each dog object you create. But the the class itself isn't the person or the dog. It just a blueprint.

The Dog Class

To get a clearer idea, let’s create a class called Dog. It’ll be the blueprint for each dog object we're going to create.

class Dog:
    pass

This class doesn’t have any attributes (data) or methods (behaviors) yet. That’s why we use pass — to say, “we’ll fill this in later.”

Creating Objects (Instances)

A class is useless unless you create objects from it. The Dog class isn’t a dog itself — it’s just the blueprint. Let’s create some dogs:

tommy = Dog()
dora = Dog()
luca = Dog()

tommy,dora and luca are objects created from the Dog blueprint. But it feels empty. But they feel kind of empty, don’t they?

Adding Attributes

Let’s add some attributes (data) that differentiate each dog:

  • Name

  • Breed

  • Color

  • Age

It’d be cool if we could pass these values when creating a dog object — just like we pass arguments to a function.

And guess what? That’s exactly how it works!

But, how do we add those attributes? Can we do it the way we add parameters to functions? Let’s try:

class Dog(name,breed,color,age):
    pass

If you run this, you’ll get a NameError because the syntax is invalid. We didn’t need to define parameters like this for functions, so why not here?

That’s because this seat is reserved for something else — for parent class(es), which we’ll cover in a future article.

The __init__() Constructor Method

While we can’t put attributes in the class header, we can put them inside a special method called __init__.

The word init comes from initial, which means beginning. This method is automatically called when you create an object.

Here’s how the __init__ method looks:

class Dog:
    def __init__(self):
        pass

The __init__ method is called the moment you create an object from a class. You don’t need to call this method explicitly. In other words, creating an object from a class is how you call its __init__ method.

Let's try it.

class Dog:
    def __init__(self):
        print('This is the Dog class')

tommy = Dog()
dora = Dog()
luca = Dog()

Output:

This is the Dog class
This is the Dog class
This is the Dog class

Cool, right?

What is self?

Everyone finds self weird at first sight. If you already have a clear idea, feel free to skip this. But if you're confused, just put aside everything you’ve heard about self and read with a clear mind.

This is a super simple concept — we often make it complicated ourselves.

Think of self like a backpack that carries everything an object owns — its attributes (like name, age) and behaviors (like bark(), eat()).

So, if a dog wants to get its name, it just opens its backpack and grabs it: self.name.

But wait — dogs don’t carry backpacks to store their data and actions. They carry it all on themselves. That's why we call it self, though you can use any word.

Every method inside a class must have self as the first parameter.

Placing Attributes

Now let’s use parameters to assign data when we create a dog.

class Dog:
    def __init__(self,name,breed,color,age):
        pass

Now you can create dogs like this:

tommy = Dog('tommy','Labrador Retriever','Black',3)
dora = Dog('dora','German Shepherd','Black & Tan',6)
luca = Dog('luca','Doberman','Black & Rust',4)

But there’s a problem. These values are only accessible inside the __init__ method. You can’t use them in other methods or outside the class.

class Dog:
    def __init__(self,name,breed,color,age):
        print('Name: ',name)
        print('Breed: ',breed)
        print('Color: ',color)
        print('Age: ',age)

If we want to access them anywhere else, we have to store them inside the object — in the backpack.

class Dog:
    def __init__(self,name):
        self.name = name

This lines of code says: take the name from outside and store it inside the object as an attribute called name.

Want to store it under a different name? Totally fine.

class Dog:
    def __init__(self,name):
        self.dog_name = name

You can access attributes both inside and outside the class.

class Dog:
    def __init__(self,name,breed,color,age):
        self.name = name

tommy = Dog('tommy','Labrador Retriever','Black',3)
print(tommy.name)

It’s your turn now: store the other attributes (breed, color, age) in the object too.

class Dog:
    def __init__(self,name,breed,color,age):
        self.name = name
        # Your turn: store the rest!

Answer

Final Thoughts

I honestly didn’t want to use the dog example in this article series — it’s used everywhere. So I spent some time thinking of a different example. But eventually, I realized that choosing another one would only make things worse.

So… I’m going with the dog example anyway.

And I made a little excuse to convince myself:
“It’s not the examples you use that set you apart — it’s how well you explain them, even to a complete beginner.”

Anyway, in the next article, we’ll focus on methods. Stay tuned!

You can subscribe to the newsletter to get new articles delivered straight to your inbox the moment I post them. Follow me on GitHub for more contents— and maybe Instagram too.

You can support my effort by reacting to and commenting on this article. Share it with someone who would find it valuable.

3
Subscribe to my newsletter

Read articles from Beenuka Hettiarachchi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Beenuka Hettiarachchi
Beenuka Hettiarachchi