Python-concise naming convention

Share a big cow's artificial intelligence tutorial. Zero-based! Easy to understand! Funny and humorous! Hope you join the artificial intelligence team too! Please click http://www.captainbed.net

Package (directory)

It should be a short, lowercase name. It is best not to use underscores.

 

Module (file)

It should be a short, lowercase name. If the underscore can improve readability, it can be added.

 

class

Always use initial capitalized word strings, such as MyClass. Inner classes can use additional leading underscores.

 

Functions and methods

Function names should be lowercase, and underscores can be used to increase readability. Such as: my_function.

 

Parameters of 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 a reserved keyword, it is usually better to use a suffix underscore than to use abbreviations or strange spellings.

 

variable

Variable names are all lowercase, and the words are connected by underscores. Such as my_variable.

1. Regardless of whether it is a class member variable or a global variable, the m or g prefix is ​​not used.
2. Private class members use a single underscore prefix to identify more public members and fewer private members.
3. Variable names should not contain type information, because Python is a dynamically typed language.

 

Global variable

The variable name is capitalized, and each word is connected by an underscore, such as: GLOBAL_VAR_NAME.

 

constant

All letters of the constant name are capitalized, and the words are connected by underscores, such as MAX_SIZE.

 

abnormal

Use "Error" as the suffix.

 

abbreviation

The naming should use fully spelled words as much as possible. There are two types of abbreviations as follows:

1. Commonly used abbreviations, such as XML, ID, etc., should only be capitalized when naming, such as XmlReader.
2. If there are long words in the name, abbreviate a word. In this case, conventional abbreviations should be used. E.g:

Function is abbreviated as fn
text, abbreviated as txt
object, abbreviated as obj
count, abbreviated as cnt
number, and abbreviated as num.

 

Leading suffix underscore
A leading underscore: indicates non-public.
A suffix underscore: avoid keyword conflicts.
Two leading underscores: used when naming a class attribute causes a name conflict.
Two leading and suffix underscores: "magic" (special purpose) objects or attributes, such as __init__ or __file__. It is recommended not to create such names, but to just use them.

Guess you like

Origin blog.csdn.net/chimomo/article/details/109776429