Can Python use Chinese variable names?

Hello everyone, welcome to Crossin's programming classroom!

Variable naming is a very basic operation in programming. All introductory programming books will mention it, and all programming learners will also understand it.

Let me test everyone first: which of the following variable names are acceptable in Python, and which are not?

  1. age

  2. await

  3. __abc__

  4. v index

  5. player.no

  6. 2

  7. variable 1

  8. count-2

  9. ᐖ oᐛ ng

  10. ______

  11. adfijaosdfjoadfqowenqoiqiefknyuq2ogn872u3hbasrads23jnfuyadvewnfoaoidv

Naming conventions

For Python, variable command specifications include the following:

  1. Variable names can consist of letters, numbers, and underscores, and cannot contain spaces and other special characters.

  2. Variable names must start with a letter or underscore, not a number.

  3. Variable names are case sensitive, eg age and Age are different variable names.

  4. Variable names should not repeat keywords in Python (such as if, else, while, etc.).

Many books and tutorials have said the above, and it is no problem to use it in practice. But there is a problem here:

Can there be Chinese in the variable name?

According to the above rules, it should not be possible, because Chinese does not belong to "letters, numbers and underscores".

But as long as you try it with Python, you will find that it can run:

变量 = 1
print(变量)

How is this going? Are those tutorials written wrong?

Yes, wrong. But it's also possible that they were right when they first wrote it.

Because in the early version of Python2, it is true that Chinese cannot be used as a variable name. However, it has been available for a long time, and it will be fully supported by Python3.

Not only Chinese, but other languages ​​in Unicode (such as Japanese, Russian, Thai, etc.) can also be used in variable names. But note that not all characters are acceptable, such as Chinese full-width punctuation and emoji. It can be roughly understood as: "text" in various languages ​​​​is possible.

However, whether to really name it in Chinese is a matter of opinion. For example, which one do you think is more comfortable to look at in the code, "maximum width" or "maxWidth"?

In addition, Chinese variable names may still cause coding problems in the process of code collaboration and distribution. So the use of non-ASCII characters as variable names is still discouraged.

Naming suggestions

In addition to the above-mentioned naming conventions that must be followed, variable naming also needs to pay attention to its readability and maintainability.

Here are some variable naming suggestions:

  1. Variable names should be descriptive, clearly express what the variable represents, and should not be too long.

  2. Variable names should not be prefixed with a single underscore, such as _name, which is usually used for private variables.

  3. Double underscores should not be used as prefixes and suffixes in variable names, such as __name__. This naming method is usually used for special methods and properties of classes.

Back to the first few variable names:

  1. age  ✔

  2. await ✖ This is a Python syntax reserved word

  3. __abc__ ✔ Possible but not recommended, this way of writing is usually used in the magic method of the class

  4. v index ✖ cannot have spaces

  5. player.no ✖ If player is an object, its no attribute can be called, but it cannot be used as a variable name

  6. 2num ✖ number cannot be placed first

  7. Variable 1 ✔ Chinese is ok

  8. count-2  ✖ 

  9. งᐖoᐛง ✔ This is actually a letter of some language (I don’t know which one), so it’s okay

  10. ______ ✔ Do you think this is a dividing line? No, it can also be a variable, but again not recommended

  11. adfijaosdfjoadfqowenqoiqiefknyuq2ogn872u3hbasrads23jnfuyadvewnfoaoidv ✔ Too long, ok but not recommended

To be honest, the rules of variable naming are not complicated, but how to choose an appropriate variable name when writing code often makes people bald. No wonder some people say that with ChatGPT, the biggest advantage is that you don't have to worry about naming it anymore.

In the past work and teaching process, I often see such names, see if you have been shot:

  • How convenient: a, b, c, d, if not enough, just a1, a2, a3...

  • Pinyin, full spelling is fine, but I often misspell it, make mistakes with flat tongue and tongue, front nasal sound and back nasal sound... What's more, I use the first letter of pinyin, then I can only say yyds

How do you like to name your variables? Welcome to share in the message area.


The content of this article is excerpted from Crossin's new book " Action on Code: Zero-Basic Learning of PYTHON Programming (CHATGPT Edition) ".

9cd2f9f2054de0c8fae3e6f6a87a9169.jpeg

This book strives to be easy to understand, so that zero-based "novice" who has no programming experience at all can learn Python. The content starts from the most basic steps of environment construction, and gradually goes deep into common practical applications. While explaining the knowledge points, it is equipped with corresponding code examples, so that readers can learn and practice to deepen their understanding. The book also innovatively uses ChatGPT as an aid to programming learning, leading readers to explore a new mode of learning programming in the AI ​​era.

Thank you for retweeting and liking ~

Guess you like

Origin blog.csdn.net/qq_40523737/article/details/131546310