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

Build a Representation

00:00 I want a prettier printout when I print() a Dog object. Well, I actually didn’t print it, I just showed its .__repr__().

00:06 But if you print(d), currently you’re getting the same output, which isn’t pretty at all. So I’m going to build a .__str__() method and I’ll do that inside of the parent class because then all the children can just inherit from it, and I don’t need to change anything in the child classes.

00:25 So the .__str__() method should return a user-readable string. I’m going to use an f-string for it and put some information in there. We have a .name and the .color: {self.name}

00:42 and {self.color}. It’s going to be in there somehow. And let’s make a sentence by talking about dogs and foxes. It made me think of this common sentence in typography, “The quick brown fox jumps over the lazy dog.” Maybe you’re familiar with this. It’s used in typography, if I’m not mistaken, to have a good variety of different characters and then to see how the font looks like. But it’s a fun sentence.

01:12 So my farm animals are going to be represented with this sentence because it can also include, we’ve got a .name, maybe we can put in the animal type.

01:22 And then there’s always a lazy dog on each farm, right? Let’s model it like this. The, okay, we have here a trait. So I want to add a trait to all of my animals, all the dogs, all the pigs, and all the sheep are going to have one trait, which means that inside of my .__init__() method up on the Animal class, I’m just going to add another instance attribute called .trait and also assign it: self.trait = trait.

01:52 And then I can also use it here. I can say the self.trait,

01:59 self.color, and then this would be the name of the animal. This is not really in that sentence. It won’t be a perfect representation, but I’ll put the .name in here. Well, actually we do have access to the animal type.

02:17 We have the .animal_type for each of those animals. So I’m going to put it in here, self.animal_type. And here I can also show you some more potential issues with this approach, right? So now I’m accessing .animal_type through the instance, which means that if I change it here, this output is going to change.

02:37 Let’s try that out in a second. But first, finish the sentence: The quick, brown .animal_type—let’s say fox—

02:47 and then I give it a .name and then say, “jumps over the lazy dog.”

02:56 Okay, so this is just for fun for me. There’s probably, definitely better representations of animals, but I’m going to keep myself entertained while I code this. So here you go.

03:05 A little typography reference in that code. Okay, let’s take a quick break, and then in the next lesson, try out what the potential issue is with the way that I’m referencing .animal_type in the .__str__() method.

Avatar image for David Rotthoff

David Rotthoff on Oct. 24, 2023

“The quick brown fox jumps over the lazy dog” is used in typography because it uses all 26 characters in the English language alphabet.

Avatar image for Martin Breuss

Martin Breuss RP Team on Oct. 25, 2023

That’s right! Thanks for adding that info David! :)

Here’s the Wikipedia link for the pangram. I also like some of the alternatives they mention there, e.g.:

Sphinx of black quartz, judge my vow

:)

Become a Member to join the conversation.