Bird
0
0

Which of the following is the correct syntax to define the __repr__ method inside a Python class?

easy📝 Syntax Q12 of 15
Python - Magic Methods and Operator Overloading
Which of the following is the correct syntax to define the __repr__ method inside a Python class?
Adef __repr__(self): self.return 'object info'
Bdef repr(self): return 'object info'
Cdef __repr__(self): print('object info')
Ddef __repr__(self): return 'object info'
Step-by-Step Solution
Solution:
  1. Step 1: Check method name and parameters

    The method must be named __repr__ and take self as parameter.
  2. Step 2: Verify return statement

    __repr__ must return a string, not print or use invalid syntax.
  3. Final Answer:

    def __repr__(self): return 'object info' -> Option D
  4. Quick Check:

    Correct __repr__ syntax returns string [OK]
Quick Trick: __repr__ must return a string, not print it [OK]
Common Mistakes:
  • Omitting underscores in __repr__
  • Using print instead of return
  • Wrong method name without underscores

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes