Variable naming is another subject of debate among programmers. How can a name be more readable, writable and unambiguous? Opinions vary.

In this installment of “Why Python”, we’re going to dive into the fray by focusing on concatenation in variable naming, and the question we want to answer is — why does Python recommend snake-like naming?

First, for single characters or words (e.g., a, a, PYTHON, Cat), when used as variable names, there are roughly all lowercase, all uppercase, and the first letter uppercase. When these situations occur in a programming language, they are generally expressed in the same way as in English.

However, programming languages often need to use more than one word or symbol to make variable names more meaningful. English has a habit of using Spaces to separate words, but this can cause problems in programming languages, so programmers have created an alternative:

  • Snake Case
  • Camel Case
  • Hungarian Nomenclature (HN Case)
  • Pascal case
  • Spinal Case
  • Free Nomenclature (Studly Caps)
  • The snake nomenclature of the hump

In general, these nomenclature attempts to overcome the whitespaces between words in order to string different words together to create the effect of a new “word.”

I drew a mind map that roughly distinguishes the nomenclature:

If you rank it in terms of audience and popularity, the top two are undoubtedly the hump and the serpentine.

We can briefly compare their advantages and disadvantages:

  • Readability: Serpentine nomenclature uses underscores to make the spacing larger and easier to read. Hump nomenclature provides compact variable names and saves line width
  • Writability: Hump nomenclature is case-sensitive and does not introduce additional identifiers; Serpentine nomenclature is in lower case, which is relatively convenient for input
  • Ambiguity: For certain abbreviations such as HTTP, RGB, DNS, etc., it is customary to use all capital letters, but if you strictly follow these two nomenclature, you have to use only capital letters or all lowercase letters, which can be “disruptive” and sometimes even awkward. If all caps are left, the IDE may not recognize it, and a wave hint will appear instead

Thus it can be seen that they each have advantages and disadvantages, but which side is not overwhelming. Personally, I’m a bit of a snake nominative, but I can switch easily when I need to use a hump (for example, when writing Java).

Note that Python also recommends using camel names for class names, Type variables, and exception names. The value lower_case_with_underscores is recommended for package names, module names, method names, and common variable names.

So why does Python recommend snake-like naming?

The biggest reason is historical. The snake-like naming system originated in the 1960s, before it even had a name. Python borrows the name lower_case_with_underscores from C, that is, the lowercase name that underscores.

It wasn’t until the early 2000s that people in the Intel and Ruby communities started calling it “snake_case,” the snake.

There are a number of programming languages today that recommend using snake nomenclature in some situations, but Python was one of the first to do so and is one of the most widely used.

Here’s a list of Python’s preferences on Wikipedia:

Second, and more important, is Python’s unique preference for the underscore “_”.

For example, expressions like _xx, __xx, xx_, __xx__, and so on can be found everywhere, and there are even special cases where a single underscore _ is used as a variable. It seems that underlining, as a link between words, is part of this tradition.

Finally, I saw an explanation: Python is named after a snake because it is a Python…

How do you feel about these three explanations? Do you like or dislike the snake nomenclature? Welcome to leave a message.

At the end: This article is part of the “Why” series (from Python Cat), which focuses on the syntax, design, and development of Python, and tries to illustrate the magic of Python by asking “why” questions. Some of the topics will be introduced to the video version, please watch in B station, watch the address: video address

Public account [Python Cat], this number is a series of high-quality articles, such as Why Python series, cat star philosophy cat series, Python advanced series, good book recommendation series, technical writing, quality English recommendation and translation, etc., welcome to pay attention to oh.