Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - Magic Methods and Operator Overloading
What will be the output of this code?
class Dog:
    def __repr__(self):
        return 'Dog()'

d = Dog()
print(repr(d))
A<__main__.Dog object at 0x...>
BDog()
CA dog
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand repr() calls __repr__ method

    The repr() function calls the object's __repr__ method to get its official string representation.
  2. Step 2: Check class Dog's __repr__

    Dog class defines __repr__ returning 'Dog()', so repr(d) returns 'Dog()'.
  3. Final Answer:

    Dog() -> Option B
  4. Quick Check:

    repr() calls __repr__ = 'Dog()' [OK]
Quick Trick: repr() calls __repr__ method for object string [OK]
Common Mistakes:
  • Confusing repr() with str()
  • Expecting print to call __repr__
  • Thinking code raises error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes