Skip to content

Understanding the __repr__ Function in Python

[

When Should You Use .repr() vs .str() in Python?

One of the most common tasks that a computer program performs is to display data. The program often displays this information to the program’s user. However, a program also needs to show information to the programmer developing and maintaining it. The information a programmer needs about an object differs from how the program should display the same object for the user, and that’s where .__repr__() vs .__str__() comes in.

A Python object has several special methods that provide specific behavior. There are two similar special methods that describe the object using a string representation. These methods are .__repr__() and .__str__(). The .__repr__() method returns a detailed description for a programmer who needs to maintain and debug the code. The .__str__() method returns a simpler description with information for the user of the program.

The .__repr__() and .__str__() methods are two of the special methods that you can define for any class. They allow you to control how a program displays an object in several common forms of output, such as what you get from the print() function, formatted strings, and interactive environments.

In this tutorial, you’ll learn how to differentiate .__repr__() vs .__str__() and how to use these special methods in the classes you define. Defining these methods effectively makes the classes that you write more readable and easier to debug and maintain. So, when should you choose Python’s.__repr__() vs .__str__?

In Short: Use .__repr__() for Programmers vs .__str__() for Users

Python classes have a number of special methods. These methods have a double leading underscore and a double trailing underscore in their names. You can informally refer to them as dunder methods because of the double underscores in their names.

The special methods .__repr__() and .__str__() both return string representations of the object. A string representation is a string that shows information about the object. You can tailor this information for different audiences, such as program users or your fellow programmers.

Like with other special methods with leading and trailing double underscores in their names, you can define these methods for any class.

The reason there are two methods to display an object is that they have different purposes:

  • .__repr__() provides the official string representation of an object, aimed at the programmer.
  • .__str__() provides the informal string representation of an object, aimed at the user.

The target audience for the string representation returned by .__repr__() is the programmer developing and maintaining the program. In general, it provides detailed and unambiguous information about the object. Another important property of the official string representation is that a programmer can normally use it to re-create an object equal to the original one.

The .__str__() method provides a string representation targeted to the program’s user, who may not necessarily be a Python programmer. Therefore, this representation enables any user to understand what the object is about by displaying a more simplified and user-friendly version of the object’s information.

Let’s take a look at some code examples to illustrate the difference between .__repr__() and .__str__().

class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"Point(x={self.x}, y={self.y})"
def __str__(self):
return f"A point with coordinates (x={self.x}, y={self.y})"
p = Point(2, 3)

Now, if we were to print the object p:

print(p)

The output would be: A point with coordinates (x=2, y=3). This is the result of calling the .__str__() method of the Point class.

However, if we were to use the repr() function and pass the object p as an argument:

print(repr(p))

The output would be: Point(x=2, y=3). This is the result of calling the .__repr__() method of the Point class.

As you can see, the .__str__() method returns a more human-readable and user-friendly representation of the object, while the .__repr__() method returns a detailed and unambiguous representation that can be used by programmers for debugging and re-creating the object.

In conclusion, the choice between using .__repr__() and .__str__() depends on the intended audience of the string representation. If the representation is meant to be used by programmers for debugging or re-creating objects, use .__repr__(). If the representation is meant for end-users or non-programmers, use .__str__().

Defining these special methods allows you to control how your objects are displayed and provides a clear distinction between the information needed by programmers and the information needed by end-users. It also enhances the readability and maintainability of your code.

By understanding the differences between .__repr__() and .__str__(), you can effectively use them to create more user-friendly and informative outputs for your Python programs.