In program design, Duck type (English: Duck typing) is a style of dynamic type. In this style, an object's valid semantics are not inherited from a particular class or implement a particular interface, but rather by the collection of current methods and properties.
"When you see a bird walking like a duck, swimming like a duck and barking like a duck, the bird can be called a duck." ”
In my personal understanding, duck type is a syntactic sugar, it does not emphasize the type of object, or where it inherits from, only care whether it can achieve the method I need, in general, regardless of your origin, regardless of the black Cat White cat, can catch the mouse is a good cat.
This dynamic type can be very convenient to achieve polymorphism,
Do not say, any only speak the vernacular does not take the code is bullying:
1 classPerson :2 3 defGo (self):4 Print("It is walking")5 6 classBird:7 defGo (self):8 Print("It is walking")9 Ten deffunc (a): One A.go () A forIinch[Person (), Bird ()]: -Func (i)
Results:
IS wasin 0.2s]
So?
Duck Type in Python