What is the Python identifier naming convention?

爱喝马黛茶的安东尼
Release: 2020-01-06 09:43:25
Original
9388 people have browsed it

What is the Python identifier naming convention?

Simply understand, an identifier is a name, just like each of us has our own name. Its main function is to serve as a variable, function, class, module and other object The name.

The naming of identifiers in Python is not arbitrary, but must comply with certain command rules, for example:

1. Identifiers are composed of characters (A~Z and a~z) , underscore and numbers, but the first character cannot be a number.

2. The identifier cannot be the same as a reserved word in Python. Reserved words will be introduced in detail in subsequent chapters.

3. Identifiers in Python cannot contain special characters such as spaces, @, % and $.

For example, the identifiers listed below are legal:

UserID
name
mode12
user_age
Copy after login

The identifiers named below are not legal:

4word    #不能以数字开头
try     #try是保留字,不能作为标识符
$money     #不能包含特殊字符
Copy after login

4. In Python, the identifiers in Letters are strictly case-sensitive, that is to say, two identical words, if their sizes and formats are different, have completely different meanings. For example, the following three variables are completely independent and have no relationship with each other. They are independent individuals from each other.

number = 0
Number = 0
NUMBER = 0
Copy after login

5. In the Python language, identifiers starting with an underscore have special meanings, for example:

·Identifiers starting with a single underscore symbol (such as _width), indicating a class attribute that cannot be directly accessed and cannot be imported through from...import*;

· is represented by a double underscore The identifier at the beginning (such as __add) represents the private member of the class;

·The identifier starts and ends with a double underscore (such as __init__), is a private identifier.

Therefore, avoid using identifiers starting with an underscore unless required by a specific scenario.

It should also be noted that Python allows the use of Chinese characters as identifiers, for example:

PHP中文网 = "http://php.cn"
Copy after login

But we should try to avoid using Chinese characters as identifiers, which will avoid encountering many weird errors.

In addition to following the above rules when naming identifiers, the names of identifiers in different scenarios also have certain standards to follow, for example:

·When an identifier is used as a module name, it should be as short as possible and all lowercase letters should be used. Underscores can be used to separate multiple letters, such as game_mian, game_register, etc.

·When the identifier is used as the name of the package, it should be as short as possible and all lowercase letters should be used. The use of underscores is not recommended, such as com.mr, com .mr.book etc.

·When an identifier is used as a class name, it should be capitalized. For example, define a book class and name it Book.

·The class name inside the module can be in the form of "underline first letter capitalized", such as _Book;

·Function names, attribute names and method names in classes should all use lowercase letters, and multiple words can be separated by underscores;

##· Constant naming should use all capital letters, and words can be separated by underscores;

Some readers may ask, what will happen if these specifications are not followed? The answer is that the program can still run, but the advantage of following the above specifications is that you can understand the meaning of the code more intuitively. Taking the Book class as an example, we can easily guess that this class is related to books, although the class name is changed. Being a (or otherwise) does not affect program operation, but it is not usually done.

Many

python training videos are available on the python learning website. Welcome to learn online!

The above is the detailed content of What is the Python identifier naming convention?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!