Don’t Write Idiomatic Python
Write Understandable Code
It never ceases to amaze me how far so many “professional” Python programmers have come from the original emphasis Python held that allowed it to win over Perl. Here’s the Zen of Python (
import this) as a reminder:
The Zen of Python, by Tim Peters
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Flat is better than nested.
- Sparse is better than dense.
- Readability counts.
- Special cases aren’t special enough to break the rules.
- Although practicality beats purity.
- Errors should never pass silently.
- Unless explicitly silenced.
- In the face of ambiguity, refuse the temptation to guess.
- There should be one — and preferably only one — obvious way to do it.
- Although that way may not be obvious at first unless you’re Dutch.
- Now is better than never.
- Although never is often better than *right* now.
- If the implementation is hard to explain, it’s a bad idea.
- If the implementation is easy to explain, it may be a good idea.
- Namespaces are one honking great idea — let’s do more of those!
Many (some who actually peddle books for $14.95) do not even know what the Zen of Python is. This is really a shame because these same developers are falling to the exact same demise that killed Perl: they value cool more than clear. Python fundamentally won the war with Perl because it took the “one best way” instead of the “many possible ways.”
“Idiomatic” Has Lost It’s Meaning
Unfortunately I have run across enough references to “idiomatic” Python to conclude that this term has been co-opted by anyone who wants to make their way of writing Python into the best way. In fact, it is so rampant that you should actually be immediately suspicious of anything that uses the term (like companies that put “innovation” or “synergy” or “solutions” in their titles). You have been warned.
There are many examples (including unnecessary comprehensions) but here’s one small one that recently motivated this rant:
This specific example demonstrates several of these failures together.
- He names his example list
my_containerinstead of the simpler
- He unnecessarily uses
enumerate()which is uglier and slower
- He prints out
1 Larrywould be better for printing
- He uses the word
indexwhen the world uses
nfor such things
- He picks meaningless identifiers (
namewould be better)
- The rest of the world uses a simple counter loop for this
- It’s in book form and therefore immediately outdated
Here’s a version that programmers anywhere on the planet will immediately recognize and understand. This counter loop pattern is universal:
If you insist on using a Pythonism:
Which just makes me love Go all the more (since this is built into every iteration of any map).
It is truly unfortunate that Python decided to do away with the classic, well-established, completely understandable
for syntax from almost every other significant language on the planet. (Range types are just annoying and unnecessary. They provide no added clarity.)
Learn More Than Python
This is one case where the author clearly does not code in any other language. If he did he would recognize the well established counter loop pattern and use it in a way that any programmer would recognize, not just a “Python Idiomatic Developer.”
Word to the wise, please learn more than one language before calling yourself a “programmer” or “developer” or “software engineer” (a term that really angers actual engineers, by the way). You simply cannot understand programming just learning Python. You must understand compilation and strict typing as well. I suggest learning Web/Node and Go or C. By the way, this isn’t just my opinion, this is exactly what the most popular course at Harvard (CS50) does as well as MIT, Stanford and Carnegie Mellon.
Remember Sturgeon’s Law
“90% of everything is crap” (yes, potentially including this very blog post). Just don’t forget to cross-check your sources and do not assume something is correct just because a lot of people use it. For example, codecademy.com still teaches Python2 which is widely accepted to be full of dangerous constructs for beginners, most notably integer division, if you don’t know what I’m talking about be very afraid and go look it up.
Seek Professional Mentorship
Identify professional mentors as you embark on your learn-to-code journey. Online sources are great, but they are not enough. Look for actual experience and be wary of the “you can do anything” YouTube generation who are really great at self-promotion but actually suck at most things. They seek to write books — even create entire bootcamps — before receiving a single year of professional experience. I don’t enjoy sharing this warning, I wish more people who actually had experience would find the time to share it (provided they have kept themselves current).
Update Apr 15th, 2018: 79% of Python programmers hardly ever code in another language. This explains the fierce reactions in some of the comments. Python programmers are statistically prone to becoming arrogant language bigots.