90% of people don't know that Python already supports Chinese variable names!

Bald programmer 2022-06-23 17:47:48 阅读数:674


lately , I'm going through two relatively new Python Book time , They all made a serious low-level mistake !

These two books are respectively 《Python Programming : From introduction to practice 》 and 《 The programming journey of father and son 》, They're all bestsellers , All in 2020 year 10 There's a new version of the moon , All use Python3.7+ The syntax of the version .

However , In the section on naming rules for variables , They made the same mistake , It's still in use Python2 The rhetoric of the times , Mistakenly thinking that naming only supports “ Letter 、 Numbers and underscores ” The combination of .

in fact ,Python3.x Has supported comprehensive Unicode code , For example, it supports the use of Chinese as the variable name .

>>> full name ="Python cat "
>>> print(f" I am a { full name }, Welcome to your attention !")
I am a Python cat , Welcome to your attention !

Since I don't have any other samples on hand , therefore , I'm not sure how many new books still use the old rules . however , Translation books probably have this problem , in addition , Some lax domestic books , It can also be a mistake to learn from outdated materials .

In this way , I'm afraid there are some new contacts Python Classmate , It will form a wrong understanding . Although it may not cause serious problems , But it's a problem that should be avoided, and it's easy to avoid .

therefore , I think this topic is worth talking about .

There is a very common concept in programming languages , The identifier (identifier), It's often called a name (name), Used to identify variables 、 Constant 、 function 、 class 、 Names of entities such as symbols .

When defining an identifier , There are some basic rules that have to be considered :

  • What characters can it consist of ?
  • Is it case sensitive ?( That is, case sensitivity )
  • Whether it allows some special words ?( The key word / Reserved words )

For the first question , Most programming languages In earlier versions They all follow this rule : Identifiers are made up of letters 、 Numbers and underscores , And don't start with numbers . A few programming languages have exceptions , Also supports the use of $、@、% Equal special symbol ( for example PHP、Ruby、Perl wait ).

Python Early versions , Exactly 3.0 Previous version , Just follow the above naming rules . Here is the description in the official document :

identifier ::= (letter|"_") (letter | digit | "_")*
letter ::= lowercase | uppercase
lowercase ::= "a"..."z"
uppercase ::= "A"..."Z"
digit ::= "0"..."9"

Source :https://docs.python.org/2.7/reference/lexical_analysis.html#identifiers

however , This rule starts with 3.0 Since version , It's broken . The latest official document has become like this :

Source :https://docs.python.org/3/reference/lexical_analysis.html#identifiers

With the popularization of the Internet , The languages of various countries have entered the context of internationalization , Programming languages also keep pace with the times to increase the demand for internationalization .

Unicode( Unified code 、 unicode ) The coding standard is 1994 Released in , Then it was gradually accepted by the mainstream programming language . up to now , There are at least 73 Two programming languages support Unicode Variable name ( The data are based on :https://rosettacode.org/wiki/Unicode_variable_names).

2007 year , When Python In the age of planning 3.0 version , The authorities have also considered the right Unicode Coding support , therefore , The birth of important 《PEP 3131 -- Supporting Non-ASCII Identifiers》.

Source :https://www.python.org/dev/peps/pep-3131

in fact , Besides the Chinese we care about most ,Unicode Character sets also contain a lot of content .

When you name a variable , The following usages are all feasible ( Use caution , If beaten , I'm not responsible for ……):

>>> ψ = 1
>>> Δ = 1
>>> ಠ_ಠ = "hello"

in summary , some Python The book about variable naming rules is out of date , Should not be misled by it !

Python 3 As a course for modernization / International language , about Unicode Coding is well supported . As for whether to name the identifier in Chinese in the project , That's another problem ……

版权声明:本文为[Bald programmer]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/01/202201051847395464.html