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)
Pygator on Aug. 29, 2019
This is great python knowledge. But what’s the point of the previous distinction? This is both human readable and unambigous.