Classes & Objects · Question 18 · Hard
The Coin class defines both __str__ and __repr__. What is printed for the object itself and for a list holding it?
class Coin:
def __str__(self):
return "str"
def __repr__(self):
return "repr"
c = Coin()
print(c, [c])
- str [str]
- str [repr]
- repr [repr]
- repr [str]
Answer and explanation
Answer: B. str [repr]
Explanation: print uses __str__ for the object it is given directly. A list builds its own string by using __repr__ for each element, so the object inside the list shows as repr.
Previous: Every class in this diamond calls super().go(). What string does D().go() build? · Next: total is a property with only a getter. What happens when the code tries to…
Practise classes & objects
Try a fresh set of classes & objects questions with an explanation after every answer, free and without signing up: Classes & Objects practice. Or sit a full placement mock test.
More classes & objects questions
- B is a child of A. What do isinstance, a direct type comparison and issubclass…
- D inherits from both B and C, and B and C both inherit from A. Which who()…
- Every class in this diamond calls super().go(). What string does D().go() build?
- total is a property with only a getter. What happens when the code tries to…
- sell() uses self.stock -= 1 where stock starts as a class attribute, and the…
More python practice
Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge