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



VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

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.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.
