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

How and When to Use .__repr__()

In this lesson, you’ll see how to use a second dunder method that controls how ojects are converted to strings, the .__repr__():

Python
class Car:
    def __init__(self, color, mileage):
        self.color = color
        self.mileage = mileage

    def __repr__(self):
        return '__repr__ for Car'

    def __str__(self):
        return '__str__ for Car'

You’ll learn when __repr__ is used and how it is different from .__str__(). The next lesson will cover the differences between the two in more detail.

Become a Member to join the conversation.