original link:python-naming Convention (turn)
Filename
Full lowercase, underline allowed
Package
It should be a short, lowercase name. If the underline can improve readability you can join. such as MyPackage.
Module
The same as the specification of the package. such as MyModule.
Class
Always capitalize a word string with the first letter. such as MyClass. An inner class can use an extra leading underline.
Functions & Methods
The function name should be lowercase, and underline-style words can be used to increase readability. such as: Myfunction,my_example_function.
* Note *: Mixed case is only allowed when this style has taken advantage of the time to maintain backward compatibility.
Parameters for functions and methods
Always use "self" as the first parameter of an instance method. Always use "CLS" as the first parameter of the class method.
If the parameter name of a function conflicts with the reserved keyword, it is usually better to use a suffix underline than to use abbreviations or odd spellings.
Global variables
For the From M import * Import statement, if you want to prevent global variables in the import module from being able to use the old specification, add a leading underline on the global variable.
* NOTE *: Avoid using global variables
Variable
The variable name is all lowercase, and each word is connected by an underscore. such as color = White,this_is_a_variable = 1
* Note *:
1. Either the class member variable or the global variable, neither the M or the G prefix is used.
2. Private class members are identified by using a single underscore prefix, with multiple definitions of exposed members and fewer private members.
3. Variable names should not have type information, because Python is a dynamic type language. such as Ivalue, Names_list, dict_obj and so on are bad naming.
Constant
The constant name is all uppercase, and the underline connects each word like max_overflow,total.
Abnormal
With "Error" as the suffix.
Abbreviation
Name should try to use all spelling words, the abbreviation is as follows two kinds:
1. Commonly used abbreviations, such as XML, IDs, and so on, should be named only the first letter, such as Xmlparser.
2. The name contains a long word, which is abbreviated to a word. You should use the abbreviated method of the contract moralize.
For example:
function abbreviation for FN
Text abbreviated to TXT
Object abbreviated to OBJ
The count abbreviation is CNT
Number abbreviation is NUM, etc.
Leading suffix underline
A leading underline: non-public.
A suffix underline: avoid keyword conflicts.
Two leading underscores: used when naming a class attribute that causes a name conflict.
Two leading and suffix underscores: "Magic" (with special diagram) objects or attributes, such as __init__ or __file__. Never create such names, but use them.
* NOTE *: There is some controversy about the use of underscores.
The
specific naming method
mainly refers to the system reserved word naming method in __xxx__ form. This kind of naming can also be used in projects, meaning that variables in this form are read-only, and this form of class member functions is not overloaded as much as possible. such as
Class Base (object):
def __init__ (self, id, parent = None):
self.__id__ = id
self.__parent__ = parent
def __message__ (self, MsgId):
# ... Slightly
The __id__, __parent__, and __message__ are all using the system reserved word naming method.
Attach: Google python naming conventions
Module_name, Package_name, ClassName, Method_name, Exceptionname, Function_name, Global_ Var_name, Instance_var_name, Function_parameter_name, Local_var_name.