Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Adding Coat Color (Solution)

00:00 Here I am back in the Dog class. The task was to add an instance attribute, .coat_color, that should be a string. That was actually the whole task.

00:16 And then test it out. All right, let’s go ahead and do that. I’ll start off by adding the instance attribute. Where you add instance attributes is in this standard .__init__() method.

00:28 So additionally, to .name and .age, you want a third attribute that someone needs to pass when creating a dog, and that’s going to be .coat_color.

00:39 Adding it to the parameters here in the method definition. And that also means that if you are creating a Dog instance, you’re going to have to now pass .coat_color.

00:49 But so far I’m not doing anything with it. So I will add a new line where I will assign it back to the instance under an instance attribute that I’ll name the same.

00:59 You know, it could be something else, but it usually makes sense to keep this the same because it makes the code more understandable. So I will assign the attribute that you pass in when creating a Dog instance to self.coat_color.

01:15 I think that’s already it. Let’s give it a spin. I will rerun this file pressing F5 in IDLE. And then again, it gives me this interactive way to explore this some more.

01:26 So I’m going to now create a new dog. If I create philo again with only two arguments, then this should fail. Fail. And there you go. It says that it’s missing one required positional argument, coat_color.

01:40 Perfect. That’s what I was expecting. So let’s give it the right age of 5 and then the coat_color of brown. I forgot to close the string.

01:53 One more time. It’s a little strange because it’s not very well visible. It’s a bit better now. So we have a Dog instance that I’m passing three arguments, a name, an age, and a coat_color.

02:06 And this should now create fine. It does. And now philo.coat_color gives me back brown. Perfect. So now there was like an f-string to print this out.

02:25 f"{philo.name}'s"

02:31 and their "coat is {philo.coat_color}". So this is just an f-string now, and that should print out the right string, Philo's coat is brown.

02:45 I want to add this .coat_color also to the .__str__() method because currently when I print(philo), it still just gives me information about their name and age.

02:55 And now I’m just going to add a bit of information at the end of the .__str__() method.

03:03 And their coat is

03:07 self.coat_color.

03:12 It was a little long, so it doesn’t all display in here, but if I save and rerun this, I should now be able to print(philo) and also get its coat color right away.

03:23 print(philo). Philo is 5 years old and their coat is brown. And that finishes this exercise. We created an instance attribute called .coat_color and then went ahead and tested it as well.

03:40 Looks good. Are you feeling warmed up? In the next lesson, you’ll wrap up the review exercises and then move on to the challenges.

Become a Member to join the conversation.