Recall & Review
beginner
What is duck typing in Python?
Duck typing means that Python cares about what an object can do, not what type it is. If it behaves like a duck (has duck-like methods), Python treats it like a duck.
Click to reveal answer
beginner
Explain the phrase: "If it walks like a duck and quacks like a duck, it is a duck."
This phrase means that if an object has the methods and behavior expected, Python will accept it regardless of its actual type.
Click to reveal answer
intermediate
How does duck typing differ from traditional type checking?
Traditional type checking verifies an object's type before using it. Duck typing skips type checks and focuses on whether the object has the needed methods or properties.
Click to reveal answer
beginner
Give a simple Python example that shows duck typing.
Example:
class Duck:
def quack(self):
print("Quack!")
class Person:
def quack(self):
print("I'm quacking like a duck!")
def make_it_quack(thing):
thing.quack()
make_it_quack(Duck()) # prints Quack!
make_it_quack(Person()) # prints I'm quacking like a duck!Click to reveal answer
intermediate
Why is duck typing useful in Python programming?
Duck typing makes code flexible and easier to extend. You can use different objects as long as they have the right behavior, without strict inheritance or type checks.
Click to reveal answer
What does duck typing focus on in Python?
✗ Incorrect
Duck typing cares about what an object can do, not its exact type.
Which of these best describes duck typing?
✗ Incorrect
Duck typing means using an object if it has the needed methods, without strict type checks.
In duck typing, what happens if an object lacks a required method?
✗ Incorrect
If the object does not have the method, Python raises an AttributeError when you try to use it.
Which statement is true about duck typing?
✗ Incorrect
Duck typing allows flexibility by focusing on behavior, not inheritance or strict types.
What is a common error when duck typing fails?
✗ Incorrect
If an object does not have the expected method, Python raises an AttributeError.
Explain duck typing in your own words and why it is useful in Python.
Think about how Python decides if an object can be used, not what it is.
You got /4 concepts.
Write a simple Python function that demonstrates duck typing with two different classes.
Create two classes with the same method and a function that calls that method on any object.
You got /4 concepts.