The name Duck Typing comes from the phrase:
“If it looks like a duck and quacks like a duck, it’s a duck”
In this case, we call method
len() gives the return value from
__len__ method. Here
__len__ method defines the property of the class
The object’s type itself is not significant in this we do not declare the argument in method prototypes. This means that compilers can not do type-checking. Therefore, what really matters is if the object has particular attributes at run time. Duck typing is hence implemented by dynamic languages. But now some of the static languages like Haskell also supports it. But, Java/C# doesn’t have this ability yet.
Example: Now, lets look demonstrates how an object be used in any other circumstances until it is not supported.
fly with wings fly with fuel
Traceback (most recent call last): File "/home/854855e5570b9ce4a9e984209b6a1c21.py", line 20, in obj.fly() AttributeError: 'Fish' object has no attribute 'fly'
In this example, we can see a class supports some method we can modify it or give them new functionality. Duck-typing emphasis what the object can really do, rather than what the object is.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course