Santiago Profile picture
29 Nov, 11 tweets, 3 min read
One of the things I love about Python: Duck Typing + EAFP.

A short thread that will change the way you write code.

2. 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.

In other words: the functionality of an object is more important than its type. If the object quacks, then it's a duck.
3. Duck Typing is possible in dynamic languages.

Look at the attached example.

Notice how "Playground" doesn't care about the specific type of the supplied item. Instead, it assumes that it supports the bounce() function.
4. To make the Playground example work, we don't need inheritance or interfaces.

Code is simpler without a complex hierarchy of types.

Let's see another example.
5. In Python, the len() function returns how many elements are in a list.

This function works with any class that implements a __len__() method, regardless of its type.

This is an example of Duck Typing in action.

But let's see another example.
6. The attached example uses __iter__() and __next__() to allow our Computer class to work with the standard for-loop construct.

Again, no need to implement specific types. If it quacks, it's a duck.
7. Duck Typing is closely related to the "It's Easier to Ask for Forgiveness than Permission" (EAFP) principle.

Look at the two attached samples.

They accomplish the same but use a different principle.
8. "Look Before You Leap" (LBYL) principle: It first checks whether "x" and "y" exist, then uses them.

"It's Easier to Ask for Forgiveness than Permission" (EAFP) principle: It assumes that "x" and "y" exist. Raises an exception otherwise.

Python prefers EAFP.
9. Exceptions have been traditionally costly in most programming languages. This is one of the reasons LBYL is so common.

In Python, exceptions are cheap. The overhead of using them is negligible.
10. Combining Duck Typing and EAFP makes your code cleaner, explicit and fosters reusability and flexibility without cluttering it with abstractions.

If you aren't familiar with this style, I'd recommend you take a deeper dive.

It may help you to write better code!
A good follow up addition to this thread:

On How "try - except" blocks in Python are inexpensive when no exception is thrown.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Santiago

Santiago Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @svpino

22 Nov
For one second, let's forget the idea of a central database.

Imagine a product that doesn't store your data. Instead, you keep that information. You allow others to use it at your convenience and close it whenever you want to.

Let's talk about one example. ↓
2. Think about those people that have built excellent profiles using @kaggle.

They have participated in many competitions, shared their knowledge, and built impressive curriculums.

What would happen if Kaggle decides to ban them?
3. This is not science fiction.

Every single company out there can ban you if they decide to do so.

Maybe it is justified, maybe it isn't, but that's beyond the point.

What happens then?
Read 18 tweets
19 Nov
Deploying a machine learning model is not a trivial task.

Here are some of the questions you may have to answer every time: ↓
1. What's the input format expected by your service?

2. How can we validate the input is valid? What's the appropriate action if it isn't.

3. What transformations are needed to turn the service's input into the model's input?
4. What transformations are needed to turn the model's output into the service's output?

5. Do we need to allow for batch processing of data?

6. How much time do we have to return an answer?
Read 6 tweets
10 Nov
Over the last few months, I've introduced three main improvements to how I build machine learning models.

Keep in mind that my job is focused on Computer Vision, and I mostly use TensorFlow and Keras.

Here are the highlights: ↓
First, I replaced image generators with the tf.data API.

This change alone has had a major impact on training time. But it doesn't stop there:

• My code is much cleaner
• A data pipeline is easily reusable

tf.data is a must.
Together with loading data, I used image generators to perform data augmentation.

Now, I try to make data augmentation part of the model using Keras' preprocessing layers.

These augmentations now happen in the GPU. This is another nice boost!
Read 6 tweets
8 Nov
If I were to start building a career in machine learning today, here is where I'd focus:

1. Python from the get go.
2. Learn how to build software.

I'd take my time here and avoid rushing into the "machine learning" specific stuff.

Something interesting happens here: ↓
A lot of people start learning software development because they want to get into machine learning.

Then they realize that machine learning is not what they care about.

This is great: there are many ways to build a successful career in the software industry.
As soon as you're comfortable, here is what I'd tackle next:

3. Machine learning fundamentals
4. Hands-on machine learning

I like to cover these at the same time, instead of one after the other: learn some theory, then apply it right away.

Something to keep in mind:
Read 12 tweets
2 Nov
Here is the story of one of those hidden issues with machine learning models that books don't tell you about.

This happened in real life: ↓
Imagine you are building a computer vision model.

It goes something like this:

1. Load a dataset of images
2. Train a model with those images
3. Export the final model

Pretty standard stuff.
To make it more specific, let's imagine that you are using OpenCV to load the images from the disk.

Something like the attached screenshot.

Nothing fancy here, right?
Read 11 tweets
29 Oct
A step-by-step guide to your first Computer Vision problem and 10 questions you should answer after that.

No math and no fancy degrees. If you can read Python, you can do this.

If this is your first time looking at this type of problems, my goal is for you to get familiar with some of the high-level ideas.

There will be some hand-waving, but don't worry about that. Focus on the process and the big pieces.
Here is a @DeepnoteHQ notebook with the code and the entire documentation.

You can open it and run it yourself step by step:

deepnote.com/@svpino/MNIST-…
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Thank you for your support!

Follow Us on Twitter!

:(