Duck typing is a concept of object-oriented programming, where the type of an object is not described by its class, but by the presence of certain methods or attributes.

It is in a sense, the application of ducks tests in computer science, in reference to James Whitcomb Riley's poem:

" When I see a bird that walks like a duck, swims like a duck and quacks like a duck, I call that bird a duck. "

Duck typing is characteristic of object-oriented scripting languages ​​like Python, Groovy, PHP and Ruby.

When duck typing the program at runtime to check whether an object supports the corresponding features. This leads, as with all dynamic type systems to increased flexibility, but also reduces the possibility of static compile-time to find errors in the program. In languages ​​such as Java and D, and C # to version 3.0, it is necessary to specify when defining a class which interfaces to be implemented. These languages ​​, it thus does not allow you to specify after the completion of a class, the class implemented yet another interface ( even if all methods are available, and thus already provide all the functionality of the objects ) in addition.

In C function templates provide a way Ducktyping at compile time. Older GCC versions provided next to so-called Signatures. These can be as interfaces declare and use, but they are independent of the class hierarchy so that, for example, must be a duck not be declared again that they can cackle, thus implements the Signature. This corresponds to the interface structures mentioned in Go.



We consider a class derived from Bird Duck and a list of objects, which are only partially are real ducks ( Duck instances of the class):

Class Bird: # implicitly derived from '' object ''      "Birds have a name, they call in their string representation also "      def __ init__ (self, name): = name        def __ str__ ( self):          return self.__class__.__name__ ''   class Duck (bird):      " Ducks are birds that can also croak "      def quack ( self):          print str ( self) ': quack '   ducks = [ Bird ( ' Gustav '), Duck ( ' Donald ') object, ()] So we have a bird Gustav, who is not a duck, a duck Donald and some other object; Bird objects passed as their String representation back her class and her name. The execution of

For duck ducks in:      try:          duck.quak ()      except AttributeError:          print ' No Duck: ' duck produces the output

No Duck: Bird Gustav   Donald Duck: Quack Duck No: The same result would

For duck ducks in:      if hasattr ( duck ' quack '):          duck.quak ()      else:          print ' No Duck: ' duck time; is crucial for the duck typing that we have the class not (as in the following code ) to explicitly check:

If isinstance ( duck, Duck):      duck.quak () But ... only the presence of the relevant method. Let us add now a class Frog added (even frogs can croak ), with the Duck has no family relationship:

Class Frog:      def quack ( self):          print str ( self) ': quack '   ducks.append ( Frog ()) As the output

No Duck: Bird Gustav   Donald Duck: Quack Duck No:   <__main__.Frog instance at ...>: quack shows, the frog is accepted as a duck - he can croak. Derived from Duck he need not be.

Other examples are in Python StringIO objects that allow it to read a string as a file, or iterating over the lines of a text file, as the elements of a list.