In this article

On the usage of Pythonic idioms

Ways to ensure that all code is equal and no code is more equal than others.

It needs to make zense
An enlightened Python master teaches disciples about the zen of Python

It needs to make zense

Design patterns are conventions that make it easier to communicate and reason about commonly occurring problems and their solutions. The Python community takes the idea of using conventions one step further, and argues that code should be “Pythonic”. Alexandru et al. explore what that means in practice.

Why it matters

The Python community cares so much about having a single “right” way to design and implement solutions, that it has even coined a special word for it: “Pythonic”.

The term appears to be very widespread: the word “Pythonic” is mentioned in a staggering 90% of all Stack Overflow threads about Python.

Nevertheless, the concept itself has never been thoroughly researched.

How the study was conducted

The authors aim to learn how well the concept of “Pythonic” code is understood by developers and how widespread concrete Pythonic idioms are.

First, they interviewed 13 Python developers with diverse backgrounds and levels of Python experience.

Then, the authors compiled a list of idioms by manually analysing presentations, books, and online resources by renowned Python developers.

Each of these idioms can be represented using an abstract syntax tree. The authors therefore mined 1,000 popular Python repositories on GitHub for these abstract syntax trees to determine how prevalent these idioms are in practice.

What discoveries were made

Correct usage of Pythonic idioms is considered to very important and sets expert Python developers apart from less experienced Python developers.

Developers

All interviewed developers were familiar with the term “Pythonic” and agreed that Pythonic code is desirable.

Pythonic code is generally understood to be elegant and readable code that makes good use of features offered by the language and its standard libraries. This often results in more succinct code, improved readability, and better performance.

Developers usually learn about the best way to write Python code by reading books and code written by others, attending conferences, discussions with colleagues, and consulting Stack Overflow.

Actually applying this knowledge to new code can be hard at first though, and may require years of practice before one can readily implement them. This is probably why the “Pythonic-ness” of code can be used to estimate its developer’s level of Python expertise.

Writing pythonic code is usually mandatory in open-source projects. However, code in closed-source projects often has lower standards and is more likely to be “merely” documented.

Idioms

The table below shows the idioms that were found in the 1,000 projects, together with the number of projects in which they were used and the total number of usages across those projects.

Idiom# ProjectsTotal
List comprehension86675,466
Dict comprehension146796
Generator expression70933,038
Decorator765114,545
Simple magic methods75978,376
Intermediate magic methods41713,255
Advanced magic methods1902,613
finally50418,900
with848143,453
enumerate68119,453
yield67256,687
lambda664109,600
collections.defaultdict3142,930
collections.namedtuple2622,211
collections.deque1801,698
collections.Counter1331,074
@classmethod51822,220
@staticmethod48711,552
zip55414,929
itertools128839
functools.total_ordering3082

You can find more thorough explanations of these idioms on the paper’s companion website.

Summary

  1. Pythonic code is elegant, readable, and makes good use of features offered by the language and its standard libraries

  2. Solutions that are implemented pythonically are often more readable and more performant

  3. A developer’s level of expertise can be estimated by their usage of Pythonic idioms

  4. Code in open-source projects is often more Pythonic than code in closed-source projects

  5. Check out the list of idioms in this article or on the paper’s companion website