Duck typing

Duck typing
Type systems

Type safety
Inferred vs. Manifest
Dynamic vs. Static
Strong vs. Weak
Nominal vs. Structural
Dependent typing
Duck typing
Latent typing
Linear typing
Uniqueness typing

This box: view · talk · edit

In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. The name of the concept refers to the duck test, attributed to James Whitcomb Riley (see history below), which may be phrased as follows:

When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.[1]

In duck typing, one is concerned with just those aspects of an object that are used, rather than with the type of the object itself. For example, in a non-duck-typed language, one can create a function that takes an object of type Duck and calls that object's walk and quack methods. In a duck-typed language, the equivalent function would take an object of any type and call that object's walk and quack methods. If the object does not have the methods that are called then the function signals a run-time error. It is this action of any object having the correct walk and quack methods being accepted by the function that evokes the quotation and hence the name of this form of typing.

Duck typing is aided by habitually not testing for the type of arguments in method and function bodies, relying on documentation, clear code, and testing to ensure correct use.


Concept examples

Consider the following pseudo-code for a duck-typed language:

function calculate(a, b, c) => return (a+b)*c

example1 = calculate (1, 2, 3)
example2 = calculate ([1, 2, 3], [4, 5, 6], 2)
example3 = calculate ('apples ', 'and oranges, ', 3)

print to_string example1
print to_string example2
print to_string example3

In the example, each time the calculate function is called, objects without related inheritance may be used (numbers, lists and strings). As long as the objects support the "+" and "*" methods, the operation will succeed. If translated to Ruby or Python, for example, the result of the code would be:

[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
apples and oranges, apples and oranges, apples and oranges, 

Thus, duck typing allows polymorphism without inheritance. The only requirement that function calculate needs in its variables is having the "+" and the "*" methods.

The duck test can be seen in the following example (in Python). As far as the function in_the_forest is concerned, the Person object is a duck:

class Duck:
    def quack(self):
    def feathers(self):
        print("The duck has white and gray feathers.")
class Person:
    def quack(self):
        print("The person imitates a duck.")
    def feathers(self):
        print("The person takes a feather from the ground and shows it.")
    def name(self):
        print("John Smith")
def in_the_forest(duck):
def game():
    donald = Duck()
    john = Person()

In statically typed languages

Certain usually statically typed languages such as Boo and the version 4 release of C# have extra type annotations[2][3] that instruct the compiler to arrange for type checking of classes to occur at run-time rather than compile time, and include run-time type checking code in the compiled output. Such additions allow the language to enjoy most of the benefits of duck typing with the only drawback being the need to identify and specify such dynamic classes at compile time.

Comparison with other type systems

Structural type systems

Duck typing is similar to but distinct from structural typing. Structural typing is a static typing system that determines type compatibility and equivalence by a type's structure, whereas duck typing is dynamic and determines type compatibility by only that part of a type's structure that is accessed during run time.

The Objective Caml and Scala languages use structural type systems.


Interfaces can provide some of the benefits of duck typing but duck typing is distinct in that no explicit interface is defined. For example, if a third party Java library implements a class you are not allowed to modify, you cannot use an instance of the class in place of an interface you have defined yourself, whereas duck typing would allow this. Again, all of an interface must be satisfied for compatibility.

Templates or generic types

Template, or generic functions or methods apply the duck test in a static typing context; this brings all the advantages and disadvantages of static versus dynamic type checking in general. Duck typing can also be more flexible in that only the methods actually called at run time need to be implemented, while templates require implementation of all methods that cannot be proven unreachable at compile time.

Examples include the languages C++ and D with templates, which developed from Ada generics.


One issue with duck typing is that it forces the programmer to have a much wider understanding of the code he or she is working with at any given time. In a strongly and statically typed language that uses type hierarchies and parameter type checking, it's much harder to supply an unexpected object type to a class. For instance, in Python, you could easily create a class called Wine, which expects a class implementing the "press" attribute as an ingredient. However, a class called Trousers might also implement the press() method. With Duck Typing, in order to prevent strange, hard-to-detect errors, the developer needs to be aware of each potential use of the method "press", even when it's conceptually unrelated to what he or she is working on.

In essence, the problem is that, "if it walks like a duck and quacks like a duck", it could be a dragon doing a duck impersonation. You may not always want to let dragons into a pond, even if they can impersonate a duck.

Proponents of duck typing, such as Guido van Rossum, argue that the issue is handled by testing, and the necessary knowledge of the codebase required to maintain it.[4][5]

Criticisms around duck typing tend to be special cases of broader points of contention regarding dynamically typed versus statically typed programming language semantics.[citation needed]


Alex Martelli made an early (2000) use of the term in a message to the comp.lang.python newsgroup. He also highlighted misunderstanding of the literal duck test, which may indicate that the term was already in use:

In other words, don't check whether it IS-a duck: check whether it QUACKS-like-a duck, WALKS-like-a duck, etc, etc, depending on exactly what subset of duck-like behaviour you need to play your language-games with.


In C#

In C# 4.0 the compiler and runtime collaborate to implement dynamic member lookup. Notice how parameter duck is declared dynamic in method InTheForest of class Program.

namespace DuckTyping
  using System;
  public class Duck
    public void Quack()
    public void Feathers()
      Console.WriteLine("The duck has white and gray feathers.");
  public class Person
    public void Quack()
      Console.WriteLine("The person imitates a duck.");
    public void Feathers()
      Console.WriteLine("The person takes a feather from the ground and shows it.");
  internal class Program
    private static void InTheForest(dynamic duck)
    private static void Game()
      Duck duck = new Duck();
      Person person = new Person();
    private static void Main()

In Cobra

In addition to static typing, Cobra allows one to declare objects of type 'dynamic' and send any message to them. At run-time the message passing will either succeed or throw an exception. The 'dynamic' type is the default for object variables and method arguments when a type has not been explicitly declared for them. This feature was inspired by Objective-C.[6]

In ColdFusion

The web application scripting language ColdFusion allows function arguments to be specified as having type any. For this sort of argument, an arbitrary object can be passed in and method calls are bound dynamically at runtime. If an object does not implement a called method, a runtime exception is thrown which can be caught and handled gracefully. In ColdFusion 8, this can be picked up as a defined event onMissingMethod() rather than through an exception handler. An alternative argument type of WEB-INF.cftags.component restricts the passed argument to be a ColdFusion Component (CFC), which provides better error messages should a non-object be passed in.

In Common Lisp

Common Lisp provides an object-oriented extension (Common Lisp Object System, or shorter CLOS). The combination of CLOS and Lisp's dynamic typing make duck typing a common programming style in Common Lisp.

With Common Lisp one also does not need to query the types, since at runtime an error will be signaled when a function is not applicable. The error can be handled with the Condition System of Common Lisp. Methods are defined outside of classes and can also be defined for specific objects.

;; We describe a protocol for 'duck-like' objects. Objects with methods for
;; these three generic functions may be considered 'ducks', for all intents
;; and purposes -- regardless of their superclass.
(defgeneric quack (something))
(defgeneric feathers (something))
;; Implementation of the protocol for class DUCK.
(defclass duck () ())
(defmethod quack ((a-duck duck))
  (print "Quaaaaaack!"))
(defmethod feathers ((a-duck duck))
  (print "The duck has white and gray feathers."))
;; But we can also implement it for PERSON, without inheriting from DUCK.
(defclass person () ())
(defmethod quack ((a-person person))
  (print "The person imitates a duck."))
(defmethod feathers ((a-person person))
  (print "The person takes a feather from the ground and shows it."))
;; IN-THE-FOREST does not need to be polymorphic. Its 'duck' argument is
;; anything that implements the duck protocol above.
(defun in-the-forest (duck)
  (quack duck)
  (feathers duck))
;; GAME can also just be a regular function.
(defun game ()
  (let ((donald (make-instance 'duck))
        (john (make-instance 'person)))
    (in-the-forest donald)
    (in-the-forest john)))

The usual development style of Common Lisp (by using a Lisp REPL like SLIME) allows also the interactive repair:

? (defclass cat () ())
? (quack (make-instance 'cat))
> Error: There is no applicable method for the generic function:
>          #<STANDARD-GENERIC-FUNCTION QUACK #x300041C2371F>
>        when called with arguments:
>          (#<CAT #x300041C7EEFD>)
> If continued: Try calling it again
1 > (defmethod quack ((a-cat cat))
        (print "The cat imitates a duck."))
1 > (continue)
"The cat imitates a duck."

This way software can be developed by extending partially working duck typed code.

In JavaScript

var Duck = function(){
    this.quack = function(){alert('Quaaaaaack!');};
    this.feathers = function(){alert('The duck has white and gray feathers.');};
    return this;
var Person = function(){
    this.quack = function(){alert('The person imitates a duck.');};
    this.feathers = function(){alert('The person takes a feather from the ground and shows it.');}; = function(){alert('John Smith');};
    return this;
var in_the_forest = function(duck){
var game = function(){
    var donald = new Duck;
    var john = new Person;

In Lua

Lua supports duck typing as part of the Metatable weak-typing system. Any reference to a table's member function is checked dynamically at run-time. If an object does not implement the requested function, a run-time error is produced. If a data member is requested but does not exist, a nil value is returned.

local duck_mt = { }
local duck_methods = {}
duck_mt.__index = duck_methods
function duck_methods:quack ( )
        print ( "Quaaaaaack!" )
function duck_methods:feathers ( )
        return "The duck has white and gray feathers."
function new_duck()
        return setmetatable ( { } , duck_mt )
local person_mt = { }
local person_methods = {}
person_mt.__index = person_methods
function person_methods:quack ( )
        print ( "The person imitates a duck." )
function person_methods:feathers ( )
        return "The person takes a feather from the ground and shows it."
function person_methods:get_name ( )
        return self.firstname .. " " .. self.lastname
function new_person ( t )
        return setmetatable ( t or {} , person_mt )
local function in_the_forest ( duck )
local donald = new_duck()
local john = new_person { firstname="John", lastname="Smith" }

In Objective-C

Objective-C, a cross between C and Smalltalk, allows one to declare objects of type 'id' and send any message to them, like in Smalltalk. The sender can test an object to see if it responds to a message, the object can decide at the time of the message whether it will respond to it or not, and if the sender sends a message a recipient cannot respond to, an exception is raised. Thus, duck typing is fully supported by Objective-C.

In Perl

Perl looks for method definitions in package set with bless function.

use strict;
package Duck;
sub hatch {
        bless \(my $self), shift;
sub quack {
        print "Quaaaaaack!\n";
sub feathers {
        print "The duck has white and gray feathers.\n";
package Person;
sub give_birth {
        bless \(my $self), shift;
sub quack {
        print "The person imitates a duck.\n";
sub feathers {
        print "The person takes a feather from the ground and shows it.\n";
package main;
sub in_the_forest
        my $duck = shift;
my $duck = Duck->hatch();
my $person = Person->give_birth();
in_the_forest( $duck );
in_the_forest( $person );


PHP uses variable functions[7] and variable variables to perform duck typing. Sometimes call_user_func[8] or similar are also used.

class Duck{
        function quack(){ echo "Quack\n";}
        function fly(){ echo "Flap, Flap\n";}
class Person{
        function __construct($name){$this->name=$name;}
        function quack(){echo "{$this->name} walks in the forest and imitates ducks to draw them\n";}
        function fly(){ echo "{$this->name} takes an airplane\n";}
function QuackAndFly($obj){$obj->quack(); $obj->fly();}
QuackAndFly(new Duck());
QuackAndFly(new Person("Jules Verne"));
Flap, Flap
Jules Verne walks in the forest and imitates ducks to draw them.
Jules Verne takes an airplane.

In Python

Duck typing is heavily used in Python. The Python's Glossary defines duck typing as follows:

Pythonic programming style that determines an object's type by inspection of its method or attribute signature rather than by explicit relationship to some type object ("If it looks like a duck and quacks like a duck, it must be a duck.") By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using type() or isinstance(). Instead, it typically employs the EAFP (Easier to Ask Forgiveness than Permission) style of programming.

The canonical example of duck typing in Python is file-like classes. Classes can implement some or all of the methods of file and can be used where file would normally be used. For example, GzipFile implements a file-like object for accessing gzip-compressed data. cStringIO allows treating a Python string as a file. Sockets and files share many of the same methods as well. However, sockets lack the tell() method and cannot be used everywhere that GzipFile can be used. This shows the flexibility of duck typing: a file-like object can implement only methods it is able to, and consequently it can only be used in situations where it makes sense.

The EAFP principle describes the use of exception handling. For example instead of checking to see if some purportedly Duck-like object has a quack() method (using if hasattr(mallard, "quack"): ...) it's usually preferable to wrap the attempted quacking with exception handling:

except (AttributeError, TypeError):
    print("mallard can't quack()")

Advantages of this approach are that it encourages the structured handling of other classes of errors (so, for example, a mute Duck subclass could raise a "QuackException" which can be added to the wrapper without delving more deeply into the logic of the code, and it handles situations where different classes of objects might have naming collisions for incompatible members (for example, Mallard the purported medical professional might have a boolean attribute which classifies him as a "quack=True"; an attempt to perform Mallard.quack() would raise a TypeError)).

In the more practical examples of classes which implement file-like behavior the use of Python's exception handling facilities is generally preferred for handling a wide variety of I/O errors that can occur due to numerous environmental and operating system issues that are outside of the programmer's control. Here again the "duck typing" exceptions can be caught in their own clauses alongside the OS, I/O or other possible errors without complicated testing and error checking logic.

Simply stated: provided you can perform the job, we don't care who your parents are.

In PowerShell

This is the concept example from the beginning of the page.

Function calculate($a, $b, $c) {
    return ($a+$b)*$c
calculate 1 2 3
"$(calculate (1, 2, 3) (4, 5, 6) 2)"
calculate 'apples ' 'and oranges, ' 3

In Ruby

class Duck
  def quack
    puts "Quaaaaaack!"
  def feathers
    puts "The duck has white and gray feathers."
class Person
  def quack
    puts "The person imitates a duck."
  def feathers
    puts "The person takes a feather from the ground and shows it."
def in_the_forest duck
def game
  donald =
  john =
  in_the_forest donald
  in_the_forest john


External links

Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Duck Typing — ist ein Konzept der Objektorientierung, das die Anwendbarkeit bestimmter Verfahren nicht an die Ableitung der Klasse von einer bestimmten Superklasse oder der förmlichen Implementierung einer Spezifikation knüpft, sondern an das Vorhandensein… …   Deutsch Wikipedia

  • Duck-Typing — ist ein Konzept der objektorientierten Programmierung, bei dem der Typ eines Objektes nicht durch seine Klasse beschrieben wird, sondern durch das Vorhandensein bestimmter Methoden. Der Name ergibt sich in Anlehnung an James Whitcomb Rileys… …   Deutsch Wikipedia

  • Duck typing — En programmation informatique, le duck typing (traduction: typage canard) est un mode d interprétation du code source utilisé par certains langages de programmation orientés objet à typage dynamique. En duck typing, la sémantique d un objet, c… …   Wikipédia en Français

  • duck typing — noun A style of dynamic typing in which an objects current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface …   Wiktionary

  • Duck (disambiguation) — Contents 1 Animals 2 Transportation 3 Computer software 4 …   Wikipedia

  • Duck Test — Sieht aus wie eine Ente, schwimmt wie eine Ente, quakt wie eine Ente: Das ist eine Ente Der Terminus Ententest (englisch duck test) beschreibt eine Methode einer analogen Begriffsbestimmung, wonach jemand versucht, die Natur eines bestimmten… …   Deutsch Wikipedia

  • Duck test — For the use of the duck test within the Wikipedia community, see Wikipedia:DUCK. A duck The duck test is a humorous term for a form of inductive reasoning. This is its usual expression …   Wikipedia

  • Dynamic typing — Dynamische Typisierung ist die Zuteilung des Typs einer Variablen zur Laufzeit eines Programms. Im Gegensatz zur statischen Typisierung verzichtet man auf eine explizite Typisierung; der Typ einer Variablen ergibt sich aus dem Typ des Werts, der… …   Deutsch Wikipedia

  • Latent typing — In computer programming, latent typing (as opposed to eager typing or manifest typing) is a style of typing that does not require (or perhaps even offer) explicit type declarations. Latent typing is heavily associated with duck typing and dynamic …   Wikipedia

  • Manifest typing — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia