Why does Python recommend the Snake-Case nomenclature?

towards-data-science

This post was originally published by Pawan Jain at Towards Data Science

Although camel-case is a good option

Regarding the naming of variables, this is another topic that can easily lead to programmer debate. How can the naming be more readable, easy to write, and clear? There are many opinions.

In this story of “Why Python” series, we will focus on the connection method in variable namings:

The question we want to answer is- why does Python recommend the snake-like nomenclature?

First of all, for single characters or words (for example A, PYTHON, BOY), when they are used as variable names, there are roughly all lower case, all upper case, and first letter upper case.

However, programming languages ​​usually need to use multiple words or symbols to express a richer meaning in variable names. English is used to using spaces to separate words, but this usage will cause some trouble in the programming language, so programmers have created other methods:

In general, these nomenclatures are to overcome the spaces between words and to connect them, and finally achieve the effect of creating a new “word”.

If you rank according to the number of audiences and popularity, there is no doubt that the top two names are camel nomenclature and snake-case nomenclature.

We can simply compare their advantages and disadvantages:

Readability:

Ease of writing

Clarity:

This shows that they have their advantages and disadvantages, but neither side is overwhelming. I prefer the snake-case nomenclature, but when you need to use camel case naming (such as when writing Java), you can also switch without barriers.

It should be noted that Python also recommends the use of camel case naming, which is in the case of class names, type variables, and exception names.

In the case of package names, module names, method names, and common variable names, it is recommended to use snake-shaped naming (lower_case_with_underscores).

So why does Python recommend the snake-like nomenclature?

1. The biggest reason is historical.

The serpentine naming method originated in the 1960s when it did not even have a specific name. After borrowing from the C language, Python named it “lower_case_with_underscores”, that is, underlined lowercase names.

It was not until the beginning of the 21st century that in the Intel and Ruby communities, people began to call it “snake_case”, which is a snake-like name.

Many programming languages ​​today recommend the use of snake-like nomenclature in certain scenarios, and Python is one of the earliest to do so and is one of the most used languages.

A list of statistics on Wikipedia shows the preference of Python for it:

Source: Wikipedia

2. Python’s unique preference for underscore “_”.

For example, writing similar to _yy, __yy, yy_, __yy__, etc. is everywhere, and there are even special cases of a single solitary underscore _ as a variable.

In this way, the underscore as a connection between words is just part of this traditional habit.

Finally, I also saw an explanation: because Python is a python, it is of course named after a snake… 🙂

How do you feel about these three explanations? Do you like snake-case nomenclature? Let me know in comments

Spread the word

This post was originally published by Pawan Jain at Towards Data Science

Related posts