liveBook · Manning

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.

How 4 Manning books explain it

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.

Read how The Art of Unit Testing 3e explains duck typing →

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.

Read how Practices of the Python Pro explains duck typing →

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

What to know first

Related concepts

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.