til

Using repr

Objects have two string representations:

(from https://github.com/SumanthRH/python-mastery/blob/main/learnings.md)

Example for __repr__:

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

    def __str__(self):
        return f"Car name: {self.name}, color: {self.color}"

    def __repr__(self):
        return f"Car(name='{self.name}', color='{self.color}')"

car = Car("Tesla", "red")
print(car)
# Car name: Tesla, color: red
print(repr(car))
# Car(name='Tesla', color='red')

same_car = eval(repr(car))
print(same_car.name)
# Tesla
print(same_car.color)
# red