Python OOP For Beginners - Instance Methods

Attributes alone won’t make our dog come alive. We need to give it some behaviors. This article is all about that.

Introduction

This is the third article in the Python OOP series. In the previous article, we added some attributes to our Dog class. Here's the final version of that code:

class Dog:

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

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

What Is a Method

So far, our dog has attributes like name, breed, color, and age. But just having attributes doesn’t make something “alive”—its actions do.

In Python, a method is simply an action or behavior.

So, what behaviors or actions does a dog typically have?

  • Eating

  • Sleeping

  • Barking

  • Running

  • Etc.

Adding Behaviors to the Dog

Don’t get overwhelmed—it’s just defining functions inside the class.

Remember what I said in the previous article? When creating a method, the first parameter must be self. You don’t need to pass it manually—Python takes care of it behind the scenes.

Let's add a method called eat.

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

    def eat(self):
        print(self.name, 'is eating.')

There is no limit. You can add more.

    def run(self):
        print(self.name, 'is running.')

    def sleep(self):
        print(self.name,'is sleeping.')

Calling a Method

It's super simple.

tommy.eat()
dora.sleep()
luca.run()

Here's the full code.

class Dog:

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

    def run(self):
        print(self.name, 'is running.')

    def sleep(self):
        print(self.name,'is sleeping.')

    def eat(self):
        print(self.name, 'is eating.')


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

tommy.eat()
dora.sleep()
luca.run()

Behaviors with Parameters

You can add any number of parameter after self.

    def eat(self, item):
        vowels = ['a','e','i','o','u']
        item = 'a '+item if item[0].lower() not in vowels else 'an '+item
        print(self.name, 'is eating',item)

Don’t worry about the logic here—it just adds “a” or “an” before the item based on the first letter.

If you're not familiar with the ternary if in Python, check out this article.

When calling this method, you just need to pass the argument for what.

Note: The first argument you provide goes to what, not to self. Python automatically passes the object as the self argument.

dora.eat('shoe')

But how do we call methods inside the class, if need to?

Calling a Method Inside the Class

This is where self might start making more sense if it still feels fuzzy. Just like how we accessed attributes using self, we can call other methods the same way.

    def eat_sleep(self):
        self.eat()
        self.sleep()

Methods vs. Functions

A method is basically a function defined inside a class. But there are some small differences:

  • A function is independent and not necessarily tied to any object or class while a method is a function that belongs to an object.

  • A function is called directly by its name while a method is called on an object.

All methods are functions, but not all functions are methods.

Final Thoughts

You’ll better understand why we even need OOP if you try doing the same thing using procedural programming.

In the next article, we'll focus on @classmethod and @staticmethod.

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.

4
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