Loading video player…

Creating the Dog Class (Trial)

00:00 Let’s try it out. Press F5 to run the file, and it puts me into the interactive mode over here, which has the information of this file of the class that I defined.

00:11 This is specific to IDLE, so if you are using a different editor, then just make sure to print it down here in the file before you run it here. I get the chance to play with it a bit, so I can create a dog.

00:24 I’ll call the dog "Philo", and it’s 12 years old. This is what I get returned. I get a Dog object. Now I should be able to print(), well, I should assign it to something, right?

00:36 That’ll make it easier.

00:41 So I assigned the Dog instance to the variable philo, and then I should be able to print(philo). So this is going to call .__str__(), and you see the f-string returned: Philo, the name of the dog is 12 years old, so that’s a nice representation.

00:58 The other task was that I should be able to call .speak() on it and pass it a sound. "Wau".

01:07 There it is, my string Philo says Wau. This is just a re-creation of the Dog class that you built in the Python Basics: Object-Oriented Programming Course.

01:17 And this is what we’re going to start with with the next review exercise, but it’s always a good review exercise also to redo some code that you’ve already built before, because if you start from scratch, there’s always going to be a little bit of thinking involved.

01:31 If you ended up with something that looks like this, then great job. This is the code that I’m going to continue working with in the next exercise as well.

01:37 Let’s get going and look at the next task.

Avatar image for Reinhard Neuwirth

Reinhard Neuwirth on Jan. 31, 2026

What I would add to the dunder __str__ method is: return f"{self.name} is a member of the species {Dog.species} and is {self.age} years old.", and alternatively f"...{self.species}..." just to highlight the point that the class attribute species is accessible both as a class attribute and as well as from all instantiations.

Avatar image for Martin Breuss

Martin Breuss RP Team on Feb. 16, 2026

Thanks, that’s a nice idea, and I agree it’s a good way to reinforce that .species is a class attribute accessible via both Dog.species and self.species.

For this particular lesson I kept .__str__() focused on the instance data so that the output stays simple and readable.

Become a Member to join the conversation.