Duck typing or duck typing is a concept related to programming that applies to certain object- oriented languages, and that has its origin in the following sentence: " If it walks like a duck and talks like a duck, then it has to be a duck ."
And what relationship do ducks have with programming? Well, it is a simile in which the ducks are objects and talk/walk methods . In other words, if a given object has the methods that interest us, that is enough for us, its type being irrelevant.
Let's define a Pato class with a hablar() method.
class Pato: def hablar(self): print("¡Cua!, Cua!")And we call the method as follows.
p = Pato() p.hablar() # ¡Cua!, Cua! Nothing new so far, but we are going to define a llama_hablar() function, which calls the hablar() method of the passed object.
def llama_hablar(x): x.hablar() When Python enters the function and evaluates x.hablar() , it doesn't care what type x belongs to as long as it has the hablar() method. This is duck typing at its best.
If you want to go deeper into the subject here I leave you an extensive tutorial talking about " Duck typing "