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

Unlock This Lesson

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

Unlock This Lesson

How and When to Use __repr__

Give Feedback

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

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.

Comments & Discussion

Become a Member to join the conversation.