Why does Python recommend snake-like nomenclature?

Regarding the naming of variables, this is another topic that is prone to programmer debate. How can the naming be more readable, easy to write and clear? Opinions vary.

In this issue of "Why Python", we will focus on the connection method in variable naming to cut into this right and wrong place. The question we want to answer is - why does Python recommend snake-like nomenclature?

First of all, for a single character or word (for example: a, A, PYTHON, Cat), when they are used as variable names, there are roughly all lowercase, all uppercase and initial capital cases. When these situations occur in programming languages, they are basically the same expression habits as English.

However, programming languages ​​often require the use of multiple words or symbols in order for variable names to convey richer meanings . English is accustomed to use spaces to separate words, but this usage can cause some trouble in programming languages, so programmers have created another method:

  • snake case
  • camel case
  • Hungarian Nomenclature (HN case)
  • Pascal case
  • Spinal case
  • Free nomenclature (studly caps)
  • Hump ​​Serpent Nomenclature

In general, these nomenclatures are to overcome the spaces between words, so as to connect different words, and finally achieve the effect of creating a new "word".

I drew a mind map to roughly distinguish these nomenclatures:

If you rank in terms of audience size and popularity, there is no doubt that the top two are hump and snake case.

We can briefly compare their advantages and disadvantages:

  • Readability: The serpentine nomenclature uses underscores to widen the word spacing, making it easier to read; the variable names of the camelcase nomenclature are compact, saving line width
  • Ease of writing: The camel case is case-sensitive, and no additional identifiers are introduced; the snake-case nomenclature is unified in lowercase, which is relatively convenient to enter
  • Ambiguity: For some abbreviated proper nouns, such as HTTP, RGB, DNS, etc., it is generally customary to use all capitals, but if these two nomenclatures are strictly followed, only the first letter must be capitalized or the whole Lowercase, this will cause some "damage" to the original meaning, and sometimes even make people feel awkward. If you keep all uppercase, the IDE may not recognize it correctly, and a wavy prompt will appear instead.

As you can see, they each have their pros and cons, but neither is overwhelming. I personally prefer snake case, but when you need to use camel case (such as when writing Java), you can switch without hassle.

It should be pointed out that Python also recommends the use of camel case, which is in the case of class names, Type variables, and exception names. In the case of package name, module name, method name and common variable name , it is recommended to use snake-shaped naming (lower_case_with_underscores).

So why does Python recommend snake-like nomenclature?

The biggest reason is historical. The serpentine nomenclature originated in the 1960s, when it didn't even have a specific name. After Python borrowed from the C language, it named it "lower_case_with_underscores", which is lowercase with underscores.

It wasn't until a few years in the early 2000s that people in the Intel and Ruby communities started to call it "snake_case", the serpentine name.

There are many programming languages ​​today that recommend the use of snake-like nomenclature in certain scenarios, and Python is one of the first to do so, and is one of the most used languages.

There's a list on Wikipedia that shows Python's preference for it:

Secondly, there is a more important reason, that is Python's unique preference for underscore "_".

For example, writing methods similar to _xx, __xx, xx_, __xx__, etc. can be seen everywhere, and there are even special cases where a single underscore _ is used as a variable. In this way, the underscore as a connection between words is precisely part of this traditional habit.

Finally, I have seen 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 or dislike the serpentine nomenclature? Welcome to leave a message.

Written at the end: This article belongs to the "Why Python" series (produced by Python Cat), which mainly focuses on topics such as Python's syntax, design, and development, and tries to show the charm of Python by starting with "why" questions. charm. Some topics will have a video version, please watch at station B, watch address: video address

The public number [ Python Cat ], this number serializes a series of high-quality articles, including why Python series, Cat Philosophy series, Python advanced series, good book recommendation series, technical writing, high-quality English recommendation and translation, etc. Welcome to pay attention .

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324033655&siteId=291194637