-1

What will be the output of this code and can anyone explain how repr method works in OOP?

class Item:
    all = []
    def __init__(self,name,age):
        self.name = name
        self.age = age
        Item.all.append(self)
    def __repr__(self):
        return f"Item('{self.name}','{self.age}')"
m1 = Item('Pranav',18)
m2 = Item('Chris',20)
print(Item.all)

Will __repr__ method automatically get called when the object is created?

The output I got was:

[Item('Pranav','18'), Item('Chris','20')]

Can anyone explain how this is happening?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Pranav
  • 1
  • `Item.all` displays the list of all created instances of `Item`, which themselves call `__repr__` when being displayed, not sure what is unclear to you – mozway Aug 28 '23 at 12:32

0 Answers0