This lesson compares
.__str__(). You’ll see when it is appropriate to use each dunder method and how some of the built-in Python functions use the two.
.__repr__() vs .__str__()
This lesson compares
All right, so we have
.__str__() and we have
.__repr__(), and they’re both called by some convention but, you know—what’s actually the difference?
00:08 Like, shouldn’t they return the same thing? How are we going to deal with this? And the answer that I’ve got for you is let’s look at what some of the Python built-in classes from the Python standard library are doing here.
So, what I’m going to do here is I’m going to import the
datetime module, and then we’re just going to create a new
date object. All right, so I just created a new
And then we’re going to try and experiment with that so we can see how it reacts, right? How does its
.__repr__() function react, and what result do we get from it?
So, let’s call
str() on that
00:53 And you can see here, we get a pretty concise representation—it’s quite readable. It looks like an ISO date format, which is kind of the standard representation for a date in string form.
Okay, ha. And now we call
repr() on the same object instance. It actually looks quite different, because now we get a more elaborate result that is really unambiguous, right?
This is not any kind of date, but we know exactly it’s a
datetime.date object and it was created in this way. And we could even copy and paste this and execute that as valid Python, and it would recreate the same object, right?
Like I said earlier, when you call, when you just inspect an object in the interpreter, that also gives you the
.__repr__(). This gives us a pretty good idea of the difference between
When you actually go to the Python documentation and do some reading on the best practices that people have come up with in the community, then you’ll learn that the
.__str__() method—it’s mainly used for giving an easy-to-read representation of your class. All right, so
.__str__() should be easy to read and it’s meant for human consumption.
So, you can see that here—it’s this ISO date. You could display that to a user and it wouldn’t be too bad. With
.__repr__(), on the other hand, it should be unambiguous.
02:28 The goal here really is to be as explicit as possible about what this object is. And it’s, I guess, more meant for internal use and something that would make things easier to debug for a developer, but you wouldn’t necessarily want to display that to a user.
And so some people actually recommend that the result of your
.__repr__() should be something like this, that would actually be valid Python and that you could just run again and would recreate the same object. Now, personally, I find that this is a really good idea, but it’s usually really hard to attain that in practice.
So, the bottom line is that you want your
.__repr__() to be unambiguous and more meant for developers, but the
.__str__()—you want that to be easy to read and potentially for human consumption.
03:15 So now, there are a couple of more interesting things that I want to talk about here because they really make this whole thing a little bit easier to understand how it works in the real world.
Become a Member to join the conversation.
Pygator on Aug. 29, 2019
interesting, i would have guessed repr returns date.datetime.today().