Python naming conventions

March 20, 2024

Why does Python recommend snake case?

CDA Data Analyst CDA Data Analyst Two people liked this article. Source: Python Cat, https://zhuanlan.zhihu.com/p/161332151

Author: 豌豆花下猫 (Cat Beneath the Pea Flowers)

Naming variables is another topic that can start an argument among programmers. What makes a name easy to read, easy to write, and clear in meaning? Opinions differ.

In this installment of “Why Python?”, we enter the debate through one narrow question: how do we join words within a variable name, and why does Python recommend snake case?

When a name consists of a single character or word, such as a, A, PYTHON, or Cat, the usual choices are lowercase, uppercase, or an initial capital. These largely follow conventions familiar from written English.

Programming languages often need several words or symbols to express a richer meaning in a name. English uses spaces between words. Since spaces cause problems inside identifiers, programmers came up with other approaches:

Snake case Camel case Hungarian notation Pascal case Spinal case Studly caps Camel snake case All of these conventions let us join several words without spaces and create what amounts to a new “word.”

I drew a mind map to show roughly how the conventions differ:

By popularity and name recognition, camel case and snake case would undoubtedly take the top two spots.

Let’s compare their strengths and weaknesses.

Readability: underscores in snake case visually separate words, making them easier to distinguish. Camel case is more compact and saves space on a line. Ease of writing: camel case signals boundaries with capitalization, without adding characters. Snake case stays lowercase, which can make typing easier. Clarity: abbreviations such as HTTP, RGB, and DNS are normally capitalized throughout. Following either convention strictly means retaining only the first capital or using lowercase throughout. Both changes can obscure the original form or simply look odd. Keeping every capital can also confuse an IDE and prompt a squiggly underline. Neither convention has an overwhelming advantage. I lean toward snake case, but I can switch comfortably to camel case when the situation calls for it, such as when writing Java.

Python also recommends a form of camel case for class names, type variables, and exception names. For packages, modules, methods, and ordinary variables it recommends snake case (lower_case_with_underscores).

So why does Python recommend snake case?

The biggest reason is historical. The convention dates back to the 1960s, when it had no particular name. Python borrowed it from C and called it “lower_case_with_underscores.”

It was only in the early years of the 21st century that people in the Intel and Ruby communities began calling it “snake_case.”

Many programming languages now recommend snake case in particular contexts. Python was among the earliest to do so and uses it in an unusually wide range of situations.

A list compiled on Wikipedia illustrates Python’s preference:

A second important reason is Python’s fondness for the underscore (_).

Names such as _xx, __xx, xx_, and __xx__ are everywhere. Even a single underscore can be a variable in its own right. Using underscores to join words fits this tradition.

I’ve also seen one last explanation: Python is a snake, so of course it uses snake case…

What do you make of these three explanations? Do you like snake case? Feel free to leave a comment.

Published July 17, 2020 at 11:02.

© 2024 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2024-03-20). Python naming conventions. https://caiborui.com/blog/2024-03-20-zh-python%E5%91%BD%E5%90%8D%E4%B9%A0%E6%83%AF-entrans

@misc{borui2024-9d8d8b56,
  author = {borui},
  title = {{Python naming conventions}},
  year = {2024},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2024-03-20-zh-python%E5%91%BD%E5%90%8D%E4%B9%A0%E6%83%AF-entrans}
}