Basic Classes/OOP behavior question

Hey folks,

I figured out the answer to this exercise but there’s something I don’t get about the output of the code below:

class Circle:
 pi = 3.14

  def __init__(self, diameter):
        self.radius = diameter / 2

  def area(self):
        return self.pi * self.radius ** 2

  def circumference(self):
        return self.pi * 2 * self.radius

  def __repr__(self):
        return ('Circle with radius {radius}'.format(radius=str(self.radius)))

medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11460)


I don’t understand why the repr method is the only one that gets called. How come we’re not getting the area, or the circumference? They’re returning something as well… perhaps the answer is obvious but I just don’t see it.


Figuring out what the answer to the exercise is shouldn’t be the primary objective. Understanding programming concepts should be. So good thing you came to the forum :slight_smile:

__repr__ is (just like __init__ a magic method (recognizable by the double underscore prefix and suffix), adding a __repr__ method overwrites the default representation (see what i did there? :wink: ) of a class instance.

so here:


when you print the instance on its own, this will “magically” call the __repr__ method. Just like __init__ is “magically” called when you instantiate a class.

which other method do you call then? You could call other methods, but you don’t do this at the moment

1 Like

Thanks for the detailed breakdown, indeed, I’d rather understand concepts over getting the right answer on most days.

My point is that I didn’t call any of the methods I created when I wrote ‘’‘print(medium_pizza)’’’ for example. Like to my mind I didn’t call the repr method any more than I called on the other two that I defined, area and circumference.

If I understand you correctly however, it seems like repr imposes itself?

but i also covered this:

it seems you struggle to understand what __repr__ does. Feel free to use google to help you find more information.

__repr__ is the default representation of an object/class instance. Has inheritance already been explained?

the default representation of an object is the following:

return "%s(%r)" % (self.__class__, self.__dict__)

which result in something like:

<class '__main__.Circle'>

which is pretty useless. So we can change this by implementing __repr__ magic method in a class

that is not the way i would put it.

Inheritance and polymorphism are up next. Gonna look into repr now.