When I heard about Duck Typing for the first time, I had to laugh.
But Python π has surprised me before, and this time was no exception.
This is another short thread 𧡠that will change the way you write code.
π
Here is the idea behind Duck Typing:
β«οΈ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's world, the functionality of an object is more important than its type. If the object quacks, then it's a duck.
π
Duck Typing is possible in dynamic languages (Hello, JavaScript fans π!)
Look at the attached example. Notice how "Playground" doesn't care about the specific type of the supplied item. Instead, it assumes that the item supports the bounce() method.
π
To make our Playground example work, we don'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't have to worry about a complex hierarchy of types.
Let's see other examples.
π
I'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.
π
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's a duck.
π
One thing to notice:
Duck Typing is closely related to the "It's Easier to Ask for Forgiveness than Permission" (EAFP) principle.
In case you are curious, I wrote about EAFP just yesterday:
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 π
π€·ββοΈ
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
This is a thread about one of the most powerful tools that make possible that knuckleheads like me achieve state-of-the-art Deep Learning results on our laptops.
π§΅π
Deep Learning is all about "Deep" Neural Networks.
"Deep" means a lot of complexity. You can translate this to "We Need Very Complex Neural Networks." See the attached example.
The more complex a network is, the slower it is to train, and the more data we need to train it.
π
To get state-of-the-art results when classifying images, we can use a network like ResNet50, for example.
It takes around 14 days to train this network with the "imagenet" dataset (1,300,000+ images.)
14 days!
That's assuming that you have a decent (very expensive) GPU.
I had a breakthrough that turned a Deep Learning problem on its head!
Here is the story.
Here is the lesson I learned.
π§΅π
No, I did not cure cancer.
This story is about a classification problem βspecifically, computer vision.
I get images, and I need to determine the objects represented by them.
I have a ton of training data. I'm doing Deep Learning.
Life is good so far.
π
I'm using transfer learning.
In this context, transfer learning consists of taking a model that was trained to identify other types of objects and leverage everything that it learned to make my problem easier.
This way I don't have to teach a model from scratch!