Python - Polymorphism and Dynamic Behavior
Which of the following Python code snippets correctly demonstrates duck typing?
hasattr to check for method presence, which fits duck typing. Options B and C check type explicitly, which is not duck typing. def quack(duck):
duck.quack()
class Duck:
def quack(self):
print('Quack!')
quack(Duck()) assumes the object has the method but does not check, which is okay but less safe than A.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions