PyGrunn: Writing idiomatic python - Jeff Knupp

Tags: pygrunn, python

(One of the summaries of the one-day 2014 PyGrunn conference in Groningen in the Netherlands).

Jeff Knupp wrote an ebook about writing pythonic code. The subtitle of his talk is towards comprehensible and maintainable code.

“Idiomatic python” doesn’t mean “idiotic snake”. It means “pythonic code”. Code written in the way the Python community has agreed it should be written. Who decided this? Well, all the python developers through the code they write, share and criticize. The patterns you see there. Who really decides is sometimes the BDFL (Guido) or a PEP.

Why would you? Three reasons:

  • Readability. This helps people read your code. You keep the “cognitive burden” low. If I have to think during reading your code, reading your code is harder. I don’t want to remember things if it isn’t necessary.

    “Cognitive burden” is the best measure of readability.

    Obligatory Knuth quote, paraphrased: write code to explain to a human what we want the computer to do, don’t write just for the computer.

  • Maintainability. Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • Correctness. If you’re the only one that can read your code, correctness is irrelevant!

    Idiomatic code doesn’t always do the right thing, It isn’t faultless. But it makes is possible for someone else to read your code and actually spot the mistake.

  • A bonus reason: “people will stop laughing at your code”.

Do it even when you write small scripts. “I’m not a programmer, I just write scripts” sounds the same as “I’m not a thief, I just steal small things”.

Do it even when you’re coming from Java and want to use bad, java-inspired, names. If you write python as java, other python programmers won’t be able to maintain your code.

Part of writing idiomatic code means staying up to date with changes in the code itself. You can do import statistics in python 3.4, giving you statistics.mean(some_list)! Likewise with list comprehension when it appeared. It makes your code more readable, but you have to know you can use it. Same with for index, value in enumerate(values). Many people don’t know enumerate() and increment some index value…

“I’m paid to write code, not to read it” is faulty. You are the one doing the most reading of the code you yourself wrote. How many times did you look at code you wrote last month without knowing why you did something?

The point in all this: python is getting more popular every day. Great. But everyone learning to write pythonic code is essential to the language’s success and their and your own success. Think about the quality of the patches for your open source projects! Think about the errors that can be prevented! logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):