IT lexicon › Programming › Duck typing

Duck typing

Programming På svenska → Updated: 2026-05-24

"If it walks like a duck and quacks like a duck, it's a duck" — in dynamic languages it's enough for an object to have the right methods/attributes, regardless of type.

Typical in Python, Ruby, JavaScript: def play(audio): only requires that audio has a play() method, not that it inherits from any AudioBase. Python's __iter__, __len__, __call__ are duck-typing protocols.

Static languages' counterpart: structural typing (TypeScript: { play(): void }) or traits/protocols (Rust, Swift). Trade-off: fast iteration vs. compile-time safety. PEP 544 (Python Protocols) brings structural typing into Python.

← Back to the lexicon