Python __repr__

Summary: in this tutorial, you’ll learn how to use the Python __repr__ dunder method and the difference between the __repr__ and __str__ methods.

Introduction to the Python __repr__ magic method

The __repr__ dunder method defines behavior when you pass an instance of a class to the repr().

The __repr__ method returns the string representation of an object. Typically, you returns a string so that if you execute it as a statement, it would return the object.

Let’s take a look at an example.

First, define the Person class with three instance attributes first_name, last_name, and age:

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age
Code language: Python (python)

Second, create a new instance of the Person class and print its string representation to the screen:

person = Person('John', 'Doe', 25) print(repr(person))
Code language: Python (python)

Output:

<__main__.Person object at 0x000001F51B3313A0>
Code language: HTML, XML (xml)

As you can see clearly that the output isn’t so useful.

To customize the string representation of the object, you can implement the __repr__ method like this:

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __repr__(self): return f'Person("{self.first_name}","{self.last_name}",{self.age})'
Code language: Python (python)

When you pass an instance of the Person class to the repr(), Python will call the __repr__ method automatically. For example:

person = Person("John", "Doe", 25) print(repr(person))
Code language: Python (python)

Output:

Person("John","Doe",25)
Code language: JavaScript (javascript)

If you execute the return string Person("John","Doe",25), it would create the person object.

When a class doesn’t implement the __str__ method and you pass an instance of that class to the str(), Python returns the result of the __repr__ method because internally the __str__ method calls the __repr__ method:

For example:

person = Person('John', 'Doe', 25) print(person)
Code language: Python (python)

Output:

Person("John","Doe",25)
Code language: JavaScript (javascript)

If a class implements the __str__ method, Python will call the __str__ method when you pass an instance of the class to the str(). For example:

class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age = age def __repr__(self): return f'Person("{self.first_name}","{self.last_name}",{self.age})' def __str__(self): return f'({self.first_name},{self.last_name},{self.age})' person = Person('John', 'Doe', 25) # use str() print(person) # use repr() print(repr(person))
Code language: Python (python)

Output:

(John,Doe,25) Person("John","Doe",25)
Code language: JavaScript (javascript)

__str__ vs __repr__

The main difference between __str__ and __repr__ method is intended audiences.

Basically, you implement __str__ method to return the string representation of an object that is human-readable.

And you implement the __repr__ method to return a string representation of an object that is machine-readable.

Summary

  • Implement the __repr__ method to customize the string representation of an object when repr() is called on it.
  • By default, __str__ calls __repr__ internally.
Did you find this tutorial helpful ?