Difficulty in getting __repr__() to work as expected

Hi! I’m following the Computer Science Career Path and encountered a problem during the OOP lesson. I’m trying to use the __ repr __() method as instructed, but whenever I print my object, I get it’s memory address, not the description I wrote for it.

class Unicorn: def __init__(self, input_name, input_age, input_color): self.name = input_name self.age = input_age self.color = input_color def __repr__(self): description = "{name} is a {age} years old, {color} colored unicorn".format(name = self.name, age = self.age, color = self.color) return description cloudette = Unicorn("Cloudette", 6, "blue") print(cloudette)

When I execute this, I get: <__ main __ .Unicorn instance at 0x7f17dde85f00>
I can get it to work only by using: print(__ repr __(cloudette)) , but I’m not sure if that’s what I’m supposed to be doing.

Thank you so much for reading this!

Change the indentation for the __repr__ function so that it is inside the class.

3 Likes

Thank you so much! :slight_smile: