What is Python __repr__?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In this article we hall learn about python \_\_repr__. Python \_\_repr__ is a special function used to represent the objects of a class in the form of a string. The built-in function repr() is used to make calls to \_\_repr__. One may define one’s very own string representation of one’s class instances by using the function \_\_repr__.

What is Python __repr__

As per the official Python documentation, \_\_repr__ is used for computing the “official” string representation of an object and is used typically while debugging. As mentioned earlier \_\_repr__ is a special Python method. A special method is a predefined function, used for enriching classes. A distinctive feature of special Python features is that they end as well as begin with double underscores.

Syntax of Python __repr__

Following is the syntax of using Python \_\_repr__:

obj is the object to be obtained as the string representation. A call to \_\_repr__ returns a string that represents the object upon which the call is made. The string representation should ideally be rich in information. The returned string representation may be used for recreating the object with the same value.

Implementation of __repr__

Let’s first define a class “Car”, which shall have a \_\_repr__ method defined in it. We may then make calls to this \_\_repr__ function from Car class using the built-in repr() function in Python.

Output

__repr__ vs __str__

\_\_str__ and \_\_repr__ are both special methods that return strings based on the state of the object. In case when \_\_str__ is missing, \_\_repr__ provides backup. Thus, one must first define a \_\_repr__, enabling one to re-instantiate an object, equivalent to the string it returns. Later, at any point in time, one may define a \_\_str__ for obtaining the string representation corresponding to the object, in a user-readable fashion.

If one prints an instance or passes it to format as in str.format, if an \_\_str__ method has been defined, it will be called, otherwise, it will be \_\_repr__ that gets used. The \_\_repr__ function is called by the built-in repr function and is what is “echoed” onto one’s Python shell screen when evaluating an expression that returns an object. Since \_\_repr__ acts as a backup for \_\_repr__, if one can write only one, one must begin with \_\_repr__.

Examples

First of all let us define a University class with three instance attributes name, country, and rating, as follows:

Now, let us construct an object of the University class and print its string representation as follows:

Output:

The default behavior is that the output shall contain the memory address of the univ object if a \_\_repr__ method has not been defined within the University class. So as to customize the string representation of the instance, you must implement the \_\_repr__ function as follows:

When you pass an object of the University class to the repr() method, Python automatically calls the \_\_repr__ function.

Output:

If a class does not implement the function \_\_str__ and an object of that class is passed to the str() function call, Python calls the \_\_repr__ function, and returns \_\_repr__ call’s return value, since internally the \_\_repr__ function calls the \_\_repr__ function. For an instance, have a look at the code block ahead:

Output:

Learn More

You want to learn more about many other cool Python concepts here.

Conclusion

  • __repr__ is a built-in method in Python that returns a string representation of an object.
  • The primary purpose of __repr__ is to provide a helpful and unambiguous representation of an object for debugging purposes.
  • __repr__ is used by developers to recreate an object by evaluating the string returned by __repr__.
  • The implementation of __repr__ can be customized by the developer to return any string that accurately represents the state of the object.
  • Understanding __repr__ is essential for any Python developer, as it allows them to create more effective and robust code.