Question 6
Back to Exercise
What is the output of the following Python program?
class Duck:
def sound(self): return "Quack"
class Robot:
def sound(self): return "Beep"
class Stone:
pass
for thing in [Duck(), Robot(), Stone()]:
print(getattr(thing, "sound", lambda: "...")(), end=" ")
print()
- A Quack Beep
- B AttributeError
- C Quack Beep None
- D Quack Beep ... Correct
Correct Answer: Option D
Duck and Robot share no parent, yet both work. getattr with a default supplies a fallback function for Stone.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!