When I heard about Duck Typing for the first time, I had to laugh.

But Python https://abs.twimg.com/emoji/v2/... draggable="false" alt="🐍" title="Schlange" aria-label="Emoji: Schlange"> has surprised me before, and this time was no exception.

This is another short thread https://abs.twimg.com/emoji/v2/... draggable="false" alt="🧵" title="Thread" aria-label="Emoji: Thread"> that will change the way you write code.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
When I heard about Duck Typing for the first time, I had to laugh.But Python https://abs.twimg.com/emoji/v2/... draggable= has surprised me before, and this time was no exception.This is another short thread https://abs.twimg.com/emoji/v2/... draggable="false" alt="🧵" title="Thread" aria-label="Emoji: Thread"> that will change the way you write code.https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">" title="When I heard about Duck Typing for the first time, I had to laugh.But Python https://abs.twimg.com/emoji/v2/... draggable="false" alt="🐍" title="Schlange" aria-label="Emoji: Schlange"> has surprised me before, and this time was no exception.This is another short thread https://abs.twimg.com/emoji/v2/... draggable="false" alt="🧵" title="Thread" aria-label="Emoji: Thread"> that will change the way you write code.https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">" class="img-responsive" style="max-width:100%;"/>
Here is the idea behind Duck Typing:

https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat">If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.

Taking this to Python& #39;s world, the functionality of an object is more important than its type. If the object quacks, then it& #39;s a duck.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
Here is the idea behind Duck Typing:https://abs.twimg.com/emoji/v2/... draggable=If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.Taking this to Python& #39;s world, the functionality of an object is more important than its type. If the object quacks, then it& #39;s a duck.https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">" title="Here is the idea behind Duck Typing:https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat">If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.Taking this to Python& #39;s world, the functionality of an object is more important than its type. If the object quacks, then it& #39;s a duck.https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">" class="img-responsive" style="max-width:100%;"/>
To make our Playground example work, we don& #39;t need to abuse from inheritance or implement interfaces.

Duck Typing makes it possible to use whatever we need to get the work done, and we don& #39;t have to worry about a complex hierarchy of types.

Let& #39;s see other examples.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
I& #39;m sure you are familiar with len(), the function that determines how many elements are in a list.

Thanks to Duck Typing, this function will work with any class that implements a __len__() method, regardless of its type.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
The attached example uses __iter__() and __next__() to prepare our Computer class with everything it needs to work with the standard for-loop construct.

Again, no need to implement specific types. If it quacks, it& #39;s a duck.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
One thing to notice:

Duck Typing is closely related to the "It& #39;s Easier to Ask for Forgiveness than Permission" (EAFP) principle.

In case you are curious, I wrote about EAFP just yesterday:

https://twitter.com/svpino/status/1315149223176929280?s=20

https://twitter.com/svpino/st... class="Emoji" style="height:16px;" src=" https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
What does this matter?

Dynamic-type advocates like myself love Duck Typing because it makes the code cleaner, explicit, and fosters reusability and flexibility without cluttering it with abstractions.

Static-type lovers have a different opinion.

But this is Python https://abs.twimg.com/emoji/v2/... draggable="false" alt="🐍" title="Schlange" aria-label="Emoji: Schlange">

https://abs.twimg.com/emoji/v2/... draggable="false" alt="🤷‍♂️" title="Achselzuckender Mann" aria-label="Emoji: Achselzuckender Mann">
You can follow @svpino.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: