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
The identifiers named below are not legal:
4word #不能以数字开头 try #try是保留字,不能作为标识符 $money #不能包含特殊字符
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
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"
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. Manypython 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
