Duck typing

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Duck typing in computer programming is an application of the duck test—"If it walks like a duck and it quacks like a duck, then it must be a duck"—to determine if an object can be used for a particular purpose. With normal typing, suitability is determined by an object's type. In duck typing, an object's suitability is determined by the presence of certain methods and properties, rather than the type of the object itself.[1]

Example[edit]

This is a simple example in Python 3 that demonstrates how any object may be used in any context, up until it is used in a way that it does not support.

class Duck:
    def fly(self):
        print("Duck flying")

class Airplane:
    def fly(self):
        print("Airplane flying")

class Whale:
    def swim(self):
        print("Whale swimming")

def lift_off(entity):
    entity.fly()

duck = Duck()
airplane = Airplane()
whale = Whale()

lift_off(duck) # prints `Duck flying`
lift_off(airplane) # prints `Airplane flying`
lift_off(whale) # Throws the error `'Whale' object has no attribute 'fly'`

In statically typed languages[edit]

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.

Templates in C++ allow the language to use compile-time duck typing. [4]

Comparison with other type systems[edit]

Structural type systems[edit]

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 OCaml, Scala, Go, Elm,[5], Gosu and PureScript languages support structural typing to varying degrees.

Protocols and Interfaces[edit]

Protocols and 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 library implements a class that cannot be modified, a client cannot use an instance of it with an interface unknown to that library even if the class does in fact satisfy the interface requirements. (A common solution to this problem is the Adapter pattern.) Duck typing would allow this. Again, all of an interface must be satisfied for compatibility.

Templates or generic types[edit]

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.

Duck Typing in scikit-learn project[edit]

A possible advantage of duck typing over inheritance is used in the API design for scikit-learn. There the interface dictates how the estimator functions are defined and for adding new functions you do not have to inherit any classes. There duck typing helps in decoupling the code from the API design hence making it easier for others to contribute to and extend the existing library. [6]

Criticism[edit]

Criticism of the term itself[edit]

Use of the term "duck typing" has been considered superfluous in light of the fact that other terms, such as dynamic binding, express the concept more clearly.[7] To proponents of static type checking, duck typing suggests the absence of typing, making its incorporation of the term typing appear incoherent.

See also[edit]

References[edit]

  1. ^ "Glossary — Python 3.7.1 documentation". docs.python.org. Retrieved 2018-11-08.
  2. ^ Boo: Duck Typing Archived October 6, 2008, at the Wayback Machine.
  3. ^ "Anders Hejlsberg Introduces C# 4.0 at PDC 2008". Retrieved 30 January 2017.
  4. ^ "Templates and Duck Typing". Retrieved 28 August 2018.
  5. ^ Czaplicki, Evan. "Core Language · An Introduction to Elm". Retrieved 30 January 2017.
  6. ^ Buitinck, Lars; Louppe, Gilles; Blondel, Mathieu; Pedregosa, Fabian; Mueller, Andreas; Grisel, Olivier; Niculae, Vlad; Prettenhofer, Peter; Gramfort, Alexandre (2013-09-23). "API design for machine learning software: experiences from the scikit-learn project".
  7. ^ Lippert, Eric (2 Jan 2014). "What is "duck typing"?". Fabulous adventures in coding. Retrieved 25 May 2016. ... the whole idea of duck typing is fundamentally incoherent ...'