Duck Typing
ELI5 — The Vibe Check
Duck typing is the ultimate 'don't ask for a resume' approach. If it walks like a duck and quacks like a duck, it's a duck. Python doesn't check your type — it just tries to call the method and if it works, great. If not, KABOOM at runtime.
Real Talk
A typing paradigm common in dynamic languages (Python, Ruby, JavaScript) where an object's suitability is determined by the presence of methods and properties rather than its actual type. No explicit interface implementation is needed — if the object responds to the expected methods, it works.
Show Me The Code
# Python duck typing
class Duck:
def quack(self): print("Quack!")
class Person:
def quack(self): print("I'm quacking!")
def make_it_quack(thing):
thing.quack() # Works with anything that has quack()
make_it_quack(Duck()) # Quack!
make_it_quack(Person()) # I'm quacking!
When You'll Hear This
"Python's duck typing means you don't declare interfaces — just call the method and hope for the best." / "Duck typing is liberating until you pass the wrong object and get a cryptic AttributeError."
Related Terms
Dynamic Typing
Dynamic typing is like a box that can hold anything — toys, food, or a cat — and you don't have to label what goes in it ahead of time.
Interface
An interface is like a job description. It says 'whatever fills this role must be able to do X, Y, and Z' without caring how they do it.
Python
Python is a programming language famous for being super readable — almost like writing in English.
Structural Typing
Structural typing is TypeScript's dating philosophy: it doesn't care about your name, only your shape.