Object-Oriented Programming

Duck Typing

English

Overview

An approach that cares about whether an object has the necessary methods, rather than its declared type.

Details

A design philosophy common in dynamic languages, based on the idea that 'if it walks and quacks like a duck, it's a duck' -- prioritizing whether the needed methods exist over explicit typing. Python's for loop works on anything that implements the iterator protocol regardless of its declared type -- this gives flexible polymorphism without explicit type declarations, but it also means you don't find out whether an object actually has the needed method until it runs, carrying a real risk of runtime errors.

More Programming terms