Overview of Dunder Methods in Python: __repr__
In the last guide, we walked through the Dunder string method and the goal of Dunder string once again is to give some pretty output for the values and the details with our class.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Now there is a very similar method and it's called Dunder repr and that's what we'll talk about in this guide. If you have never seen it before it is very similar to Dunder string with the key difference being that usually Dunder repr is used more for raw output so you usually do not format it nicely. It's something that you would do like output to your logs or to an error log or something like that.

And so this is going to be a quick guide we're going to see how we can use it. And as you'll see we're going to use it almost identically to how we use Dunder string and in your day to day development you may or may not use both of these in your class. If you have a very basic class like we have right now there's not going to be a huge need to do both of them because Dunder string is going to give you the exact same details but you can imagine a class that is quite a bit larger you may want to have the ability to have even more raw output and that's really where repr comes in.

So if I say return here and now I'm going to format our invoice so it's not just this nice sentence but instead now it's going to look more like an object output. So I'm gonna say formatted string invoice do a less than sign and you don't have to do it exactly my way I'm just showing an example of what you might see in some data logs.

And so inside of here, I can put the values and inside of that put some formatted values so self.client and then a comma and then put it in curly braces self.total and for something like this you may even want to go and name the attribute. So this is a full raw dump of all of the data in the instance of the class. So here I can say here is the data that is in the client and here is the data that we have for our total.

If I save this now and I'll duplicate this line so we can see these right next to each other. I can call this exactly like how we called string where I'm just going to call repr on the entire instance of invoice and if I run this now you can see that we have different output.

large

Now like you may have noticed there is no difference at all in regards to the implementation. The only difference is the name and then what we've done is we've changed what it represents and so that's what the common pattern you'll find in Python is that you use string for your nice output maybe something that is easy to read and then with Repr. This is the true raw data of the instance of the class.

And as you can see right here that's exactly what we've done. We have invoice from Google for 500 and then here we have the details we say invoice and then values we have client Google total 500. So my rule of thumb is whenever I use repr I wrap up all the attributes any kind of data that I know I'm going to need to use whenever I'm performing debugging and then it can be a very helpful tool both for logs and also when I'm trying to fix a bug in a program.

Code

class Invoice:
  def __init__(self, client, total):
    self.client = client
    self.total = total

  def __str__(self):
    return f"Invoice from {self.client} for {self.total}"

  def __repr__(self):
    return f"Invoice({self.client}, {self.total})"


inv = Invoice('Google', 500)
print(str(inv))
print(repr(inv))