Interfaces & Duck Typing in Python
Many languages use a formal interface keyword to declare a required set of methods a class must implement. Python has no such keyword, relying instead on duck typing by default: if an object has the methods being called on it, it works, regardless of its actual class or ancestry. For cases where a more explicit, checkable contract is genuinely useful, Python's abc module (already seen in Abstraction) and the newer Protocol class from typing both provide something closer to a formal interface.
No type checking happens by default
If a method exists and works on an object, calling it succeeds — nothing else is verified first.
This is duck typing
Any object that "quacks like a duck" (has the right methods) can be used as if it were one, with no formal declaration required.
Abstract base classes enforce a contract
At the cost of requiring explicit inheritance, as covered under Abstraction.
typing.Protocol offers a middle ground
A class can be checked against a Protocol’s shape without inheriting from it, purely by having matching methods.
Most everyday code relies on duck typing directly
ABCs or Protocols come in mainly for larger codebases that benefit from an explicit, checkable contract.
class Duck: def quack(self): print("Quack!") class Person: def quack(self): print("I'm imitating a duck!") def make_it_quack(thing): thing.quack() make_it_quack(Duck())make_it_quack(Person())Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.