Intermediate Python Intermediate Python

Magic Methods: __str__ & __repr__

Check out a free preview of the full Intermediate Python course:
The "Magic Methods: __str__ & __repr__" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces two methods that communicate details about the instance of the class, and the code needed to recreate this object.

Get Unlimited Access Now

Transcript from the "Magic Methods: __str__ & __repr__" Lesson

[00:00:00]
>> Nina: If we look at the string representation of some of these built in types that we've been working with. Let's say I have a list here, if I pass this into string I get kind of nice human readable string representation. Something we're haven't talked about is repr. If I pass the list into repr, this first string it looks a little bit different but let's try this.

[00:00:31]
>> Nina: So as we build up our objects, these simple ones can have the same str and repr, but more advanced objects you can think of the string value as the human readable string value. What would I put in here that would help other developers? For repr, it's how would I tell Python to create a new instance of this class?

[00:00:57] So it's more kind of programmatic, it's for the system, not necessarily for people to read. And we'll see this as we work with more advanced objects, not really going to talk about dates today, but this is part of the standard library, so the datetime module. And something you might see is,

[00:01:23]
>> Nina: We wanna get the current time right now. So this is the name of the module. I'll need to import datetime again just like I did from cars, import car, so this is like from datetime, import datetime. And I will call the now method on it. If I look at the str(now), it will kind of give me this human readable date.

[00:01:47] If I look at the repr, it's just gonna give me what arguments I would need to pass in to make any instance of it. So let’s make a few, let’s make a str and a repr for our class.
>> Nina: And we know that this dunder init, I’m gonna put them up here.

[00:02:19]
>> Nina: We know that this dunder init method gets called under the hood when we create a new instance of our class. If we're using this built in repr method on our class, how might we represent that in our class?
>> Speaker 2: [INAUDIBLE]
>> Nina: What's more dunder it's more double underscores.

[00:02:44] So if I have my_subaru now, before I put in either of these methods, it's gonna print out this junk. Totally unuseful, doesn't tell me anything about what's in the instance, it just says, well, it's an object of a car and it lives at this memory address. And like, what now, right?

[00:03:07] We wanna be friendly to other developers, we wanna be friendly to ourselves. So let's make a string method first and to do that, that's right, the double underscores. Now, to represent the string representation of my instance, what am I gonna need passed into this method?
>> Speaker 2: Self.
>> Nina: Self, that's right.

[00:03:32] And then, I don't wanna use print here, we wanna return a string. The print function does a call to display a value but the print function itself actually doesn't return a value. So a little bit of an important distinction here. If I assigned a value to print, the print statement, it would do the print out but if I checked the type of x, it's none.

[00:04:01]
>> Nina: Does that makes sense as to why? Yeah so in this string method we want to actually return a string. And we're gonna,
>> Nina: We're going to,
>> Nina: Just put some stuff about our car in here right? Human readable it doesn't really matter what so I can say, My car the {self.name},

[00:04:33]
>> Nina: currently,
>> Nina: Self, {self.runs},
>> Nina: Okay, and now scrolling to the top, I'm going to reload that.
>> Nina: Okay, now if I make a new car,
>> Nina: I can now call str on it.
>> Nina: And it print something out. And if I make a new_subaru,
>> Nina: Print out that, My car the subaru, right?

[00:05:29] So each instance, print out the values for each instance.
>> Nina: For repr,
>> Nina: We wanna do things a little bit differently. So if I look at the datetime,
>> Nina: The repr for the datetime, we'll see that it's the class parentheses and all of the arguments that we need to instantiate it.

[00:06:03]
>> Nina: So we would tend to do the same thing here.
>> Nina: And we would put, there's some kind of tricky Python ways to get the current class, but we're not gonna use them right now we're just gonna say car. And then we're gonna put the parenthesis here, and in here we're gonna put {self.name}.

[00:06:32]
>> Nina: Reload that.
>> Nina: So now if I make a new car,
>> Nina: (Subaru Outback),
>> Speaker 2: You capitalize car.
>> Nina: All right, capitalize car, thank you. I can now call str on it, on my new_subaru.
>> Nina: I could call repr on it as well.
>> Nina: And d is underscore special underscore methods are tied into these built-in methods in Python.

[00:07:24]
>> Nina: And as we saw earlier we keep making new instances as part of a list comprehension. So if I had a list of strings of numbers in here,
>> Nina: Oops. A list of numbers. I can say I want to assign this to a variable. Can do a list comprehension and say for num in nums call int on each of those numbers.

[00:08:01]
>> Nina: Oops I'm sorry so I made a mistake here. I made a float string here. So maybe instead of calling int, let's call float so that I can use my input. And this'll return a list of floats. And you can make new instances of your custom classes in your list comprehension as well.