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

A Complete Example & Best Practices

Give Feedback

Learn why you should at least have a __repr__ method in your classes. This lesson will show you how to adhere to the Don’t Repeat Yourself (DRY) principle when working with __repr__ by taking advantage of __class__.__name__:

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

    def __repr__(self):
        return '{self.__class__.__name__}({self.color}, {self.mileage})'.format(self=self)

Comments & Discussion

Become a Member to join the conversation.