Google Python naming convention


module_name, module

package_name, package

ClassName, class

method_name, method

ExceptionName, exception

function_name, function

GLOBAL_VAR_NAME, global variable

instance_var_name, instance

function_parameter_name, parameter

local_var_name. This variable

 

kind

Always capitalize word strings. Such as MyClass. Inner classes can use an extra leading underscore.

 

Functions & Methods    

lowercase + underscore

*NOTE*: Mixed case is only allowed when this style already prevails, in order to maintain backward compatibility.

 

Function and method parameters
If a function's parameter name conflicts with a reserved keyword, a suffix underscore is usually used

 

Global variables
For the from M import * import statement, if you want to prevent the import of global variables within a module you can use the old convention of adding a leading underscore to the global variable.
*note*: global variables should be avoided

 

variable

Lowercase, with underscores connecting words. For example, color = WHITE, this_is_a_variable = 1
*Note*:
1. Neither class member variables nor global variables are prefixed with m or g.
2. Private class members are identified with a single underscore prefix.
3. Variable names should not carry type information because Python is a dynamically typed language. Such as iValue, names_list, dict_obj, etc. are bad naming.

 

All constant
names are capitalized, and each word is connected by underscores, such as MAX_OVERFLOW, TOTAL.

 

Exceptions
are suffixed with "Error".

 

The file name is
all lowercase, can use underscore

 


应该是简短的、小写的名字。如果下划线可以改善可读性可以加入。如mypackage。

 

模块
与包的规范同。如mymodule。

 

缩写
命名应当尽量使用全拼写的单词,缩写的情况有如下两种:
1.常用的缩写,如XML、ID等,在命名时也应只大写首字母,如XmlParser。
2.命名中含有长单词,对某个单词进行缩写。这时应使用约定成俗的缩写方式。

例如:
function 缩写为 fn
text 缩写为 txt
object 缩写为 obj
count 缩写为 cnt
number 缩写为 num,等。


前导后缀下划线
一个前导下划线:表示非公有。
一个后缀下划线:避免关键字冲突。
两个前导下划线:当命名一个类属性引起名称冲突时使用。
两个前导和后缀下划线:“魔”(有特殊用图)对象或者属性,例如__init__或者__file__。绝对不要创造这样的名字,而只是使用它们。
*注意*:关于下划线的使用存在一些争议。

 

特定命名方式
主要是指 __xxx__ 形式的系统保留字命名法。项目中也可以使用这种命名,它的意义在于这种形式的变量是只读的,这种形式的类成员函数尽量不要重载。如
class Base(object):
def __init__(self, id, parent = None):
self.__id__ = id
self.__parent__ = parent
def __message__(self, msgid):
其中 __id__、__parent__ 和 __message__ 都采用了系统保留字命名法。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324823198&siteId=291194637