Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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

In this video course, you’ve learned the difference between official and informal string representations of Python objects. The special method .__repr__() returns the official string representation, which is aimed at programmers as they develop and maintain a program. The special method .__str__() returns the informal string representation, a friendlier format for the program’s user.

Now, you can distinguish between these two representations and know which one to expect in different situations. And if you haven’t been doing this already, you’ll never write another class without defining at least the .__repr__() method to provide the official string representation.

Download

Course Slides (.pdf)

3.5 MB
Download

Sample Code (.zip)

2.1 KB

00:00 In the previous lesson, I showed you how to override the dunder display methods in your own classes. This final lesson is the course summary, and I’ll also point you at other content you might find interesting.

00:12 The special methods, also known as dunder methods, determine the behavior of an object. They get invoked when you compare objects, when you create objects, and when you display objects, amongst other things.

00:24 __repr__ gets used to display information about an object to other programmers, and it’s what the REPL uses to show the results of an evaluation.

00:33 The recommended return result of __repr__ is a string that can be used to create a new copy of the object if that string gets passed to the built-in eval() function.

00:44 The __str__ method is for displaying object information to users and it’s friendlier than __repr__. It’s less strict and provides little guidance as to its content.

00:55 It’s what gets called when you print() an object out or in any other way when an object gets converted to a string. You should override the __str__ method as you need whatever works for your code.

01:09 In addition to diving deep on __repr__ and __str__ in this course, I also got a little tangential about callables and how not everything that appears to be a function in Python necessarily is.

01:20 In an earlier lesson, I dabbled a bit with f-strings. If you want to learn more, this course and tutorial will help you out. I also briefly showed you a data class.

01:30 Those got added in Python 3.7, and this guide, available in both tutorial and course formats, delves deeply.

01:38 To learn more about object-oriented programming in Python, including some dunder methods, this is a deep dive. Or if you prefer to just concentrate on the dunder methods, this tutorial shows you all of them.

01:52 That’s all for this course. Thanks for your attention.

Become a Member to join the conversation.