Home Backend Development Python Tutorial What are the legal names in python language?

What are the legal names in python language?

Jun 12, 2019 am 11:13 AM

What are the legal names in python language?

Python naming rules are as follows:

File names are all lowercase, underscores can be used

Packages should have short, lowercase names. Underlining can be added if it improves readability. Such as mypackage.

The specifications of modules and packages are the same. Such as mymodule.

classes always capitalize the first letter of a word string. Such as MyClass. Inner classes can use extra leading underscores.

Function & Method Function names should be lowercase, and underlined style words can be used to increase readability. Such as: myfunction, my_example_function.

Note: Mixed case is only allowed when this style is already dominant, in order to maintain backward compatibility.

Parameters of functions and methods

Always use "self" as the first parameter of instance methods. Always use "cls" as the first parameter of a class method. If a function parameter name conflicts with a reserved keyword, it is usually better to use a suffixing underscore than to use an abbreviation or odd spelling.

Global variables

For the from M import * import statement, if you want to prevent the global variables in the imported module from being imported, you can use the old specification and add a Leading underscore.

Note: Avoid using global variables

Variables

Variable names are all lowercase, with underscores connecting each word. For example, color = white

The variable name should not contain type information because Python is a dynamically typed language. Such as iValue, names_list, dict_obj, etc. are all bad names.

Constant

Constant names are all capital letters, with underscores connecting each word such as MAX_OVERFLOW, TOTAL.

Exceptions

are suffixed with "Error".

Specific naming method

mainly refers to the system reserved word naming method in the form of __xxx__. This kind of naming can also be used in projects. Its significance is that variables in this form are read-only. Try not to overload class member functions in this form. Such as

class Base(object):
def __init__(self, id, parent = None):
self.__id__ = id
self.__parent__ = parent
def __message__(self, msgid):
Copy after login

where __id__, __parent__ and __message__ all use system reserved word nomenclature.

The above is the detailed content of What are the legal names in python language?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles