What is repr method in Python?

In this article, we are going to understand the __repr__ method in Python. This is a special method in Python, which returns the string representation of the given object. We would explore the __repr__ method with examples so that we can understand what is the __repr__ method, and how is it different from the __str__ method.

The __repr__ method is called when the repr() function is invoked on some object. According to the documentation, the __repr__ method is used to return an official string representation of the object, which, in many cases, is a string that can be used to create another object with the same value, when passed to the eval() function.

What is the repr method in Python?

Let’s have a look at a simple example, in which, we are going to call the repr function, but since we are not going to write the __repr__ method here in our class, calling the repr() function would return some weird output, which is nothing but the default representation of the object. Let’s have a look at the program first, and then we will try to explain what is going on.

class Book:
    def __init__(self, name, pages, price) -> None:
        self.name = name
        self.pages = pages
        self.price = price

book1 = Book(“The book1”, 100, 56)
print(repr(book1))
print(book1)

As you can see, in the above program, we have a simple Book class, and then we have created a Book object, and we are trying to print it. But if you see the output of the above program, you get some weird output, which is basically not very readable for us. Notice that we have called the repr function here.

Now, let’s improve the above code, and let’s add the __repr__ method so that we can understand the concept. Also, we will call the repr() function, which will invoke the __repr__ method.

Have a look at the below-improved code –

class Book:
    def __init__(self, name, pages, price) -> None:
        self.name = name
        self.pages = pages
        self.price = price

    def __repr__(self) -> str:
        return f”Book(‘{self.name}’, {self.pages}, {self.price})”

book1 = Book(“The book1”, 100, 56)
print(repr(book1))
print(book1)

As you can see in the above program, we have the __repr__ method defined. You can see that the __repr__ method returns an f string, which is kind of an official string representation of the object. The thing is that in most cases, the string representation is such that it can be used to construct an object with the same values when passed to the eval function.

Let’s have a look at a simple program, in which, we are trying to create another object with the same values, passing the return value from the repr function to the eval.

class Book:
    def __init__(self, name, pages, price) -> None:
        self.name = name
        self.pages = pages
        self.price = price

    def __repr__(self) -> str:
        return f”Book(‘{self.name}’, {self.pages}, {self.price})”

book1 = Book(“The book1”, 100, 56)

book2 = eval(repr(book1))
print(repr(book1))
print(repr(book2))

As you can see, we could create another object with the same values. You can explore more about the eval function in Python. So here, having the __repr__ function is pretty useful at times. According to documentation, it is useful for debugging purposes.

Conclusion

In this article, we have seen the __repr__ method, which is invoked when we call the repr() function in Python. The method returns a string representation of the object. The representation should be kind of information-rich and unambiguous since it is used for debugging. You can use the __repr__ method in your class, and it should return the kind of official string representation of the object.

FAQ About repr method in Python

Q: What is repr() in Python?

Ans: We can consider that the repr() function returns a string representation of the object. If possible, the string representation returned from the repr() function, is such that we can create a new object, passing that representation to the eval function.

Q: What is the repr method in Python?

Ans: The repr method is a special method in Python, which is used to return the string representation of the object. It is invoked when the repr() function is called with the object as an argument.

Q: What is the difference between the str and repr methods?

Ans: In both cases, we have the string representation of the object. But in the case of the str, we have an informal string representation of the object, and in the case of the repr method, we have an official string representation of the object, if possible, the return value from the repr method should look like a valid python expression, that can be used to recreate the object with the same value.