Loading video player…

When to Use .__repr__() vs .__str__() in Python (Overview)

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 video course, 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__?

Download

Course Slides (.pdf)

3.5 MB
Download

Sample Code (.zip)

2.1 KB

00:00 Welcome to When to Use __repr__ versus __str__ in Python. My name is Christopher and I will be your guide.

00:10 This course is all about how Python objects get converted to information to be displayed on your screen. This process involves two special methods, __repr__ and __str__.

00:22 Throughout the course, you’ll learn how each of them gets used and when you should choose from between them. You’ll also learn how to override the default versions of them to control what their output looks like inside your own classes.

00:35 The code in this course was tested with Python 3.12. All the concepts presented here have been around for quite some time. Anything from Python 3.7 onwards will be sufficient, and if you skip the short summary of data classes, any Python 3 will work.

00:52 Have you ever stopped and thought about just what happens when you pass an object to the print() function? Under the covers, anything sent to print() is converted into a string, which is then output on your terminal.

01:04 The conversion of an object to a string is done through a built-in method called str(), and that function calls a special method on the object being converted called __str__.

01:16 From here on, I’ll be pronouncing str as string. The purpose of this conversion mechanism is to present information about an object to the user.

01:26 For example, in a Django website, the admin pages that list all the database objects use this string conversion mechanism to decide what to display for each object on the page.

01:36 And as you can override each of the object’s __str__ methods, that means you can customize just what gets displayed to the user. Python has a second mechanism for displaying information about an object, and that special method is called __repr__, or repr(), that’s short for “representation”.

01:58 And it’s the value you see as the return result inside of the REPL.

02:03 The rationale behind having two mechanisms is the string version is an informal value for users, while the repr() version is more formal and meant for programmers.

02:13 Although not enforced, it is recommended that the repr() version contain everything needed to construct a new version of the object being displayed.

02:23 In the next lesson, I’ll introduce you to the differences between these two mechanisms.

Become a Member to join the conversation.