Skip to content

Effortlessly Understand Python __repr__

[

When Should You Use .__repr__() vs .__str__() in Python?

by John Doe Mar 25, 2023

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

  • .__repr__() returns a detailed description for programmers.
  • .__str__() returns a simpler description for users.
  • Both methods can be defined for any class.

One of the common tasks that computer programs perform is to display data. The information displayed can be for the user or the programmer maintaining the code. In Python, there are two special methods, .__repr__() and .__str__(), that provide string representations of objects. These methods allow you to control how an object is displayed in different contexts. In this tutorial, you will learn about the differences between .__repr__() and .__str__() and how to use them effectively in your code.

How Can You Access an Object’s String Representations?

In Python, you can access an object’s string representation using the repr() and str() functions. The repr() function calls the .__repr__() method of an object and returns the string representation aimed at programmers. The str() function calls the .__str__() method and returns the string representation aimed at users.

Let’s consider an example where we have a class called Person:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"Person(name='{self.name}', age={self.age})"
def __str__(self):
return f"Person name: {self.name}, age: {self.age}"

In this example, we define both the .__repr__() and .__str__() methods for the Person class. The .__repr__() method returns a detailed string representation of the object, including the name and age. The .__str__() method returns a simpler string representation with just the name and age.

Now, let’s create an instance of the Person class and see how the repr() and str() functions work:

p = Person("John Doe", 25)
print(repr(p))
print(str(p))

The output will be:

Person(name='John Doe', age=25)
Person name: John Doe, age: 25

As you can see, the repr() function calls the .__repr__() method and displays the detailed string representation of the object. The str() function calls the .__str__() method and displays the simpler string representation.

Should You Define .repr() and .str() in a Custom Class?

Defining both the .__repr__() and .__str__() methods in a custom class is a good practice. By doing so, you provide different string representations for different purposes. The .__repr__() method helps programmers understand the object’s internal state and can be used to recreate the object. The .__str__() method provides a more user-friendly representation for non-programmers.

When defining the .__repr__() method, it is important to return a string that can be used to recreate the object. This means that the string should include all necessary information to initialize a new object of the same class and state.

On the other hand, the .__str__() method can be more lenient in terms of the information it provides. It should focus on presenting the key attributes or properties of the object in a human-readable format.

Here’s an example to illustrate the difference:

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"({self.x}, {self.y})"

In this example, the .__repr__() method provides a detailed representation of the Point object with the x and y coordinates. The .__str__() method, on the other hand, returns a simpler representation with just the coordinates enclosed in parentheses.

Conclusion

In conclusion, the choice between .__repr__() and .__str__() depends on the audience of the string representation. If the target audience is programmers, .__repr__() should be used to provide a detailed and unambiguous representation. If the target audience is non-programmers or users of the program, .__str__() should be used to provide a simpler and more human-readable representation.

By defining both .__repr__() and .__str__() methods in your custom classes, you can control how objects are displayed in different contexts, making your code more readable and easier to debug.


In order to ensure the accuracy of the sample codes and steps given in this tutorial, it is always recommended to try running the code on your own Python environment.