Dynamic ("Duck") Typing

The fact that an object's type is determined at run time allows functions to operate on objects of any type that support the methods used by that function.

For example we can define a rectangle type and define the < (less than) operator to compare rectangles based on their areas. This allows us to, for example, sort rectangles according to their areas:

In [1]:
class rect():
    def __init__(self,h,w):
        self.h=h
        self.w=w
    def __lt__(self,x):
        return self.h*self.w < x.h*x.w
    def __repr__(self):
        return str(self.h)+"x"+str(self.w)
    
l=[rect(1,1), rect(2,4), rect(2,2), rect(4,1)]
sorted(l)
Out[1]:
[1x1, 2x2, 4x1, 2x4]

The term duck typing comes from the saying "if it walks like a duck, swims like a duck and quacks like a duck then it's a duck".

In other words, an object's type is determined by the methods that an object implements.