This is ridiculous.

I& #39;ve been coding in Python https://abs.twimg.com/emoji/v2/... draggable="false" alt="🐍" title="Schlange" aria-label="Emoji: Schlange"> since 2014. Somehow I& #39;ve always resisted embracing one of their most important principles.

This is a short thread https://abs.twimg.com/emoji/v2/... draggable="false" alt="🧵" title="Thread" aria-label="Emoji: Thread">about idiomatic Python.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
This is ridiculous.I& #39;ve been coding in Python https://abs.twimg.com/emoji/v2/... draggable= since 2014. Somehow I& #39;ve always resisted embracing one of their most important principles. This is a short thread https://abs.twimg.com/emoji/v2/... draggable="false" alt="🧵" title="Thread" aria-label="Emoji: Thread">about idiomatic Python.https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">" title="This is ridiculous.I& #39;ve been coding in Python https://abs.twimg.com/emoji/v2/... draggable="false" alt="🐍" title="Schlange" aria-label="Emoji: Schlange"> since 2014. Somehow I& #39;ve always resisted embracing one of their most important principles. This is a short thread https://abs.twimg.com/emoji/v2/... draggable="false" alt="🧵" title="Thread" aria-label="Emoji: Thread">about idiomatic Python.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%;"/>
Python is all about readability.

One of its core principles is around writing explicit code.

Explicitness is about making your intentions clear.

Take a look at the attached code. It& #39;s one of those classic examples showing bad versus good.

See the difference?

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
There are more subtle ways in which Python encourages explicitness.

This example shows a function that checks whether two keys exist in a dictionary and adds them up if they do.

If one of the keys doesn& #39;t exist, the function returns None.

Nothing wrong here, right?

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
Well, it turns out that this function follows the "Look Before You Leap" (LBYL) principle: It first checks whether "x" and "y" exist, then uses them.

Think about this:

The code asks every time, although not having the keys is the exception, not the rule!

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
Python prefers the "It& #39;s Easier to Ask for Forgiveness than Permission" (EAFP) principle.

I rewrote the function.

Notice how now it& #39;s explicit that "x" and "y" should always be present. If they aren& #39;t, that& #39;s an exception.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
If you are used to a different language, this is probably weird.

Python uses exceptions liberally instead of constantly checking whether something is OK.

I& #39;ve critiqued this approach before, but I came around. I& #39;ve been trying hard to change the way I think about code.

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 a quick test to understand whether you are an LBYL or an EAFP coder:

https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat">Do you write a lot of if-else code blocks? You are probably doing LBYL.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="▫️" title="Weißes kleines Quadrat" aria-label="Emoji: Weißes kleines Quadrat">Do you write a lot of try-except blocks? You are probably doing EAFP.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
By the way, 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.

https://abs.twimg.com/emoji/v2/... draggable="false" alt="👇" title="Rückhand Zeigefinger nach unten" aria-label="Emoji: Rückhand Zeigefinger nach unten">
If you aren& #39;t familiar with EAFP and this thread didn& #39;t do much for you, I& #39;d recommend you take on a weekend project and read a little bit more about it.

Being a good Python developer means writing clear, idiomatic Python code and EAFP is a big part of that.
You can follow @svpino.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: