Beyond PEP 8 - Intelligible and "Pythonic" Code

PYTHON BEST PRACTICES VIDEOS

Lately, I have discovered an amazing Youtube video. It’s PyCon 2015 presentation by Raymond Hettinger, titled “Beyond PEP 8 – Best practices for beautiful intelligible code”.

I highly recommend it if you write Python code, mostly if you also review other people code:

The video is almost 1h in length, but is worth every minute. It’s a very hands-on presentation, illustrating concepts on top of a real example.

But it is not limited on being a best practices demonstration. The idea is to show that there is a lot more involved on writing beautiful code than just following the PEP 8

Even though there it’s criticized, the problem is not the PEP 8 itself; I still believe that establishing and following a code style is very important for every project that has to be written and maintained by multiple people.

But on practice, what ends up happening is that people get too attached to the code style and shift their focus to shallow problems, while leaving much serious legibility problems go by unnoticed.

Much more important than enforcing code style is making sure that code is being written in an idiomatic manner or, how it’s usually called, Pythonic way.

The concept of “Pythonic” has always sounded vague to me. Throughout the years that I have worked with the language, mostly when consulting Stack Overflow, I always heard “what is the most Pythonic way of doing this?” or “X is more Pythonic than Y”“… I started to develop a certain feeling for it but never stopped to analyse it in a formal and objective way.

The video opened my eyes for what it really means and I even dare to try to define the concept in a couple of words:

Pythonic code is that one structured in a manner encouraged by the language, following its principles (import this) and making good use of available functionality (“magic methods”, exceptions, properties, context managers, etc…)