What are the legal names in python language?

silencement
Release: 2019-06-12 11:13:33
Original
17443 people have browsed it

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!

Related labels:
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!