Duck typing
Duck typing is a typing approach in which a value’s suitability is determined by the operations or behavior it provides rather than its declared type.
Go in Practice, Second Edition
Duck typing is a typing approach that determines an object's suitability by the methods and behaviors it provides rather than by explicit nominal declarations.
- How it is used
- Uses method behavior to decide whether independent Go types can satisfy a shared interface.
- How deep it goes
- Explains interface assignment and compile-time method validation, but does not descend into runtime dispatch internals.
- Examples
- Uses Animal, Cat, Dog, and Llama types to demonstrate successful interface assignments, compile errors, and method-based repair.
- What you can do after
- Apply via Go interfaces by designing method signatures and checking type satisfaction through compiler-validated assignments.
Read how Go in Practice, Second Edition explains duck typing →
The Art of Unit Testing 3e
Duck typing is a type-compatibility approach that accepts an object based on the operations it supports rather than its declared type or class.
- How it is used
- Uses object-parameter injection to replace dependencies with controllable stubs during JavaScript tests.
- How deep it goes
- Explains matching function signatures, handwritten fakes, runtime checking, and a compile-time interface alternative.
- Examples
- Uses RealTimeProvider, FakeTimeProvider, PasswordVerifier, getDay(), and a weekend exception test.
- What you can do after
- Apply it via JavaScript object injection to substitute controllable test dependencies and exercise specific behaviors.
Practices of the Python Pro
Duck typing is a typing approach that determines an object's suitability by the presence of the required behavior rather than by its declared class or type.
- How it is used
- Enables polymorphism and swappable implementations by relying on required behavior instead of declared classes.
- How deep it goes
- Explains runtime method checks and shared interfaces, but stops short of formal interface enforcement.
- Examples
- Uses the duck analogy, FancyTire substitution, and command objects sharing execute.
- What you can do after
- Apply it via Python by designing consumers around expected methods and attributes rather than concrete classes.
Systems Programming with Zig
Duck typing is accepting a value based on whether its available operations satisfy the code's requirements rather than on an explicitly declared interface.
- How it is used
- Reuses generic code across types when each type supports the operations used in the function body.
- How deep it goes
- Defines compile-time duck typing and demonstrates operation-based validity, while noting that functions may check types manually.
- Examples
- Uses a generic sum over integers and contrasts it with an attempted sum over booleans.
- What you can do after
- Apply it via comptime type parameters to write functions reusable across types with compatible operations.
Read how Systems Programming with Zig explains duck typing →
Also covered in
- Secrets of the JavaScript Ninja, Third EditionIn early access (MEAP)
- Shipping Go: Develop, deliver, discuss, design, and go again
- The Joy of JavaScript
- The Quick Python Book, Fourth Edition
What to know first
- PythonPython is a high-level, general-purpose, dynamically typed programming language with readable syntax and support for multiple programming paradigms.
- inheritanceInheritance is a mechanism in which a derived type or descendant element receives properties or behavior from a parent, subject to rules that may allow specialization or overriding.
- methodA method is a callable function or operation associated with a type, class, or object and invoked through that association.
- moduleA module is a cohesive unit of code or computation that groups related functionality within a larger system and defines a scope or boundary for organizing or sharing it.
Related concepts
- mockinguses · Mocking is a testing technique that replaces a dependency or collaborator with a controlled substitute to simulate behavior, isolate code, or inspect interactions.
- natural language interfaceuses · A natural language interface enables interaction with a system through human-language text or speech.
- Pythonuses · Python is a high-level, general-purpose, dynamically typed programming language with readable syntax and support for multiple programming paradigms.
- iteratoruses · An iterator is an object or abstraction that provides successive values from a collection or other data source through a traversal interface.
- type checkingcontrasts with · Type checking is the process of determining whether a value or program construct satisfies the constraints of a type system.
- interfacecontrasts with · An interface is a contract or boundary that specifies the operations, members, or data shape a type or component exposes, without prescribing its complete implementation.
Questions about duck typing
What is duck typing?
Duck typing is a typing approach in which a value’s suitability is determined by the operations or behavior it provides rather than its declared type.
Which Manning books explain duck typing?
Go in Practice, Second Edition, The Art of Unit Testing 3e, Practices of the Python Pro and Systems Programming with Zig.
What should I know before learning duck typing?
Manning books that teach duck typing usually introduce Python, inheritance, method and module first.
What is the difference between duck typing and type checking?
Duck typing is a typing approach in which a value’s suitability is determined by the operations or behavior it provides rather than its declared type. Type checking is the process of determining whether a value or program construct satisfies the constraints of a type system.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



