duck typing
Duck typing is a concept used in dynamic programming languages such as Python and Ruby. It's a style of typing where the type or class of an object is determined by its method or attribute rather than explicitly specifying it. The idea is that "if it looks like a duck and quacks like a duck, it must be a duck." In other words, the suitability of an object for a particular purpose is determined by its behavior rather than its inheritance hierarchy or explicit type.
For example, if a function expects an object to have a quack()
method, it doesn't care whether the object is an instance of a Duck class specifically; as long as the object has a quack()
method, it can be used in that context.
This concept allows for more flexibility and polymorphism in dynamic languages and is commonly associated with the idea of "interfaces" in languages that don't have explicit interface constructs. This can lead to more concise and flexible code, but it also requires careful consideration of potential run-time errors since type checking is not performed until the code is executed.