Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Adding Methods to a Python Class

Give Feedback

In this video, we start adding behaviors to our dog class.

Behaviors are called Instance Methods in Python. A method is a function that belongs to a class. Just like functions, these methods can accept parameters, and they can access the attributes of the object they belong to.

class Dog:

    species = 'mammal'

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

    def speak(self, sound):
        print(f"{} says {sound}")
philo = Dog("Philo", 5)
philo.speak("Gruff gruff!")

00:00 Welcome back to our series on object-oriented programming in Python. In the last video, we created this Dog class here and we printed some information about each of our Dog objects to the screen. If you remember from the slides, we created Door objects that could be opened, closed, and locked.

00:19 I called these behaviors just to demonstrate the concepts, but in Python, these are actually called instance methods. A method is basically just a function that belongs to a class. It’s used in the exact same way as a function, except—like the initializer—we must include a self parameter. Let’s actually create some instance methods.

00:41 First, we need a way for the Dog to report its attributes in a nice, formatted way, so we’ll create the new instance method called .description()and remember that we need to include the self parameter here. And now we’ll type return "{} is {} years old" and we’ll use the handy .format() function to fill in those blanks ({}), passing in and self.age as the arguments. Notice here how we are not directly printing to the console.

01:11 Instead, we have this method returning a nice formatted string with a dog’s attributes baked into it, so we’ll need to print that out manually later on. Next, we should add a way for the dog to speak something, so let’s create a new instance method and we’ll call this one .speak().

01:29 This will take a self parameter, just like before, but I want our dog to be able to say whatever we tell him to, so let’s add another parameter called sound.

01:39 This will need to be provided when we call this method later on. Finally, we will return a formatted string where the says the sound that we pass in, just like before. Now, let’s delete all of this old code from last time and we’ll create a new Dog object called Mikey with a name of "Mikey" and an age of 6.

02:01 Just like with attributes, we can use . to access instance methods, too. Now, you might be tempted to write mikey.description(), here—and while technically, that’s not wrong, that wouldn’t actually output anything to the console.

02:15 That’s because we have the .description() method returning a formatted string, rather than directly printing it, and so this code here will get that formatted string and then do literally nothing with it. If we want to actually see it, we need to pass the return of this method call into the print() function.

02:34 Next, let’s use our .speak() method. We’ll write print(mikey.speak()) and we’ll pass in "Gruff gruff!". And now, if we right-click and choose Run Code, we’ll see that on the right we have Mikey is 6 years old, followed by Mikey says Gruff gruff!.

02:51 As you can see, our instance methods work and they can access both attributes that belong to the current object, as well as new data we pass in the form of a method parameter.

03:03 Let’s create one more instance method that will be called every time the dog celebrates his birthday. We’ll move back into our class here and we’ll call this new method .birthday().

03:14 Now in the method body, I’ll type self.age += 1, which will increase the dog’s .age attribute by 1. In other words, every time this method is called, the dog’s internal .age attribute will increase by 1. So now, at the very bottom, I can say mikey.birthday() and then I can print out mikey.description() again.

03:38 Notice how we didn’t have to put our .birthday() method call inside of a print() function. This is because the .birthday() function doesn’t actually return any data that we can print. It just changes the internal .age attribute for our Dog.

03:52 So now, if I right-click and I choose Run Code, you’ll see that we have Mikey is 6 […] Gruff gruff!, and now, Mikey is 7 years old.

04:03 At this point, we’re almost to the end of our OOP tutorial, but I have to warn you: the last part can be a little bit difficult to grasp if you don’t already have at least a basic understanding of everything we’ve covered so far.

04:16 I would strongly suggest practicing these concepts by creating your own Python classes and then instantiating them. Remember, a class can be practically any noun, so look around you and see if you can model something in your code. And ask yourself some questions when you design your class: What attributes does the object have?

04:36 Do all objects have the same value for that attribute by default, or are they all new from the time they’re created? What behaviors does this object actually exhibit? And how might these behaviors change the attributes of the object? If you’re really up for a challenge, create a class where one of its attributes is actually another custom type you define, just as we did with the Tab class storing a Page object.

05:02 If you can do that, then you should definitely have a solid grasp on this material and you’ll be all set to move on. And on the other hand, if you’re still struggling with this—don’t feel bad at all. Like I mentioned at the very beginning, this is a difficult concept for lots of new programmers to grasp, including myself.

05:20 It requires you to think about your software in a way that’s probably completely foreign to you, and this is not something that’s easy to master.

05:28 If you’re confused about something or you just forgot it, it’s a good idea to rewatch earlier parts of the series, or even read the written tutorial that this video is largely based off of over at Give yourself lots of time to practice, and once you feel that you’ve got a grasp on the concepts, come back and learn about the last concept called object inheritance.

05:51 I’ll see you there.

orinax on July 9, 2020

I took your advice and looked around the room. I saw a box of popcorn, so here is what I came up with:

class Popcorn:
    status = 0

    def __init__(self, flavor):
        self.flavor = flavor

    def description(self):
        if self.status == 0:
            return f"This is a bag of un-popped {self.flavor} popcorn."
        elif self.status == 1:
            return f"This is a bag of freshly popped {self.flavor} popcorn."
            return f"You overcooked your {self.flavor} popcorn by cooking it too much."

    def pop(self):
        self.status += 1

If you’re going through this course like me, grab some popcorn and keep on going! It comes in any flavor you like (just don’t pop it too much)!

Become a Member to join the conversation.