In Python __str__ is meant to return the object’s human-friendly representation, while __repr__ should return a valid python expression that can be used to recreate the object.
In the following sections, we’ll look at some examples, and I’ll show you how to implement these magic methods correctly for your own classes.
Table of Contents
Magic Methods
Most of the “automagical” behavior - like comparision, conversion or object initialization - of Python classes are implemented as magic methods or the so-called dunder methods. The name comes from the double underscore naming conventions.
These methods are usually not called explicitly, but get triggered automatically in certain scenarios.
str[] and repr[] vs str[] and repr[]
A good example can be the __str__[]
method, which gets called, whenever an object is passed to the str[]
builtin, or it’s twin the repr[]
builtin which calls the object’s __repr__[]
dunder method.
Both return a kind of string representation of the given object, with a slight, but important difference between the two.
What’s The Goal of str[]?
__str__[]
is meant to return a human-readable, friendly, verbose representation of the object. Think of it as a string representation that’s suitable for the end-user of your Python program.
What Is repr[] For?
In contrast __repr__[]
should always return a unique representation of the object. If it is possible the return value should be a valid Python expression that can be used to recreate the original object.
In other words eval[repr[obj]]
should equal obj
.
If this is not possible, then __repr__
should be a useful description of the object enclosed in triangular brackets - usually something like
Default Implementations
If a class does not implement a custom str[]
0 method, the default implementation will be used, which is just falling back to __repr__
on string conversion.
For this reason it is always a good idea to implement a working, meaningful __repr__
dunder method for your classes.
Containers And Recursive Data Structures
It might be surprising at first, so it’s good to note here that all containers’ and recursive data structures’ str[]
0 method uses the contained objects’ __repr__
method instead of its str[]
0 - for readability reasons.
Summary: in this tutorial, you’ll learn how to use the Python __str__
method to make a string representation of a class.
Introduction to the Python __str__
method
Let’s start with the Person
class:
Code language: Python [python]
class Person: def __init__[self, first_name, last_name, age]: self.first_name = first_name self.last_name = last_name self.age = age
The Person
class has three instance attributes including
0,Code language: Python [python]
person = Person['John', 'Doe', 25] print[person]
1, and
person = Person['John', 'Doe', 25] print[person]
Code language: Python [python]
2.Code language: Python [python]
person = Person['John', 'Doe', 25] print[person]
The following creates a new instance of the Person
class and display it:
Code language: Python [python]
person = Person['John', 'Doe', 25] print[person]
Output:
Code language: Python [python]
When you use the
4 function to display the instance of theCode language: Python [python]
person = Person['John', 'Doe', 25] print[person]
Person
class, the 4 function shows the memory address of that instance.Code language: Python [python]
person = Person['John', 'Doe', 25] print[person]
Sometimes, it’s useful to have a string representation of an instance of a class. To customize the string representation of a class instance, the class needs to implement the __str__
magic method.
Internally, Python will call the __str__
method automatically when an instance calls the
9 method.Code language: Python [python]
person = Person['John', 'Doe', 25] print[person]
Note that the
4 function converts all non-keyword arguments to strings by passing them to theCode language: Python [python]
person = Person['John', 'Doe', 25] print[person]
9 before displaying the string values.Code language: Python [python]
person = Person['John', 'Doe', 25] print[person]
The following illustrates how to implement the __str__
method in the Person
class:
Code language: Python [python]
class Person: def __init__[self, first_name, last_name, age]: self.first_name = first_name self.last_name = last_name self.age = age def __str__[self]: return f'Person[{self.first_name},{self.last_name},{self.age}]'
And when you use the
4 function to print out an instance of theCode language: Python [python]
person = Person['John', 'Doe', 25] print[person]
Person
class, Python calls the __str__
method defined in the Person
class. For example: