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.
- Type Systems:Dynamic Typing, Static Typing & Duck Typing
- Gradual typing in Python
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Index of Non-Zero elements in Python list
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to Python Libraries
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Visualizing O(n) using Python
- try and except in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.