Question discussion

What is the output of the following Python code?class A: def __str__(self): return "s" def __repr__(self): return "r" print(A(), [A()], str…

2139Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Python code?

class A:
    def __str__(self): return "s"
    def __repr__(self): return "r"
print(A(), [A()], str([A()]))
  • As [r] [s]
  • Bs [s] [s]
  • Cr [r] [r]
  • Ds [r] [r]
Correct answer: D

Explanation

print() uses __str__ for the object itself, but a list always shows its elements with __repr__, so both the list and its str() display [r].

Discussion 0

Nobody has replied yet. Start the discussion!