PEP8 Python coding standard finishing

PEP8 Python coding standard finishing


One code layout
1 indent. 4 spaces of indentation (all editors can do this), no Tap, let alone a mix of Tap and spaces.
2 The maximum length of each line is 79. You can use backslashes for newlines, preferably parentheses. The line feed point is to hit enter after the operator.
3. Two empty lines between class and top-level function definitions; one empty line between method definitions in classes; one empty line between logically irrelevant paragraphs in functions; no more empty lines in other places.

2. Documentation
1 Order of module content: module description and docstring—import—globals&constants—other definitions. The import part is arranged in the order of standard, tripartite and self-writing, with a blank line in between.
2 Do not import multiple libraries in one sentence, such as import os, sys is not recommended.
3 If you use from XX import XX to reference the library, you can omit 'module.', but there may be a naming conflict. In this case, import XX should be used.

The general principle of the use of three spaces is to
avoid unnecessary spaces.
1 Do not add spaces before various closing brackets.
2 Do not add spaces before commas, colons, and semicolons.
3 Do not add a space before the opening parenthesis of a function. Such as Func(1).
4 Do not add a space before the opening parenthesis of the sequence. Such as list[2].
5 Add a space to the left and right of the operator, do not add spaces for alignment.
6 Spaces are omitted from left and right of the assignment character used by the default parameter of the function.
7 Do not write multiple statements on the same line, although using ';' allows.
8 In the if/for/while statement, even if the execution statement is only one sentence, it must start a new line.

Four
general principles of annotation, wrong annotation is better than no annotation. So when a piece of code changes, the first thing to do is to modify the comments!
Comments must be in English, preferably a complete sentence, with the first letter capitalized, followed by a terminator, followed by two spaces to start the next sentence. In the case of a phrase, the terminator can be omitted.
1 block comment, a comment added before a piece of code. Add a space after '#'. Paragraphs are separated by only '#' lines. for example:

# Description : Module config.
#
# Input : None
#
# Output : None

2 lines of comment, add a comment after a line of code. For example: x = x + 1 # Increment x
But use this method sparingly.
3 Avoid unnecessary comments.

Five Documentation Description
1 Write docstrings for all common modules, functions, classes, and methods; non-common ones are not necessary, but can be commented (on the next line of def).
2 If the docstring needs to wrap, refer to the following example, see PEP 257 for details

"""Return a foobang

Optional plotz says to frobnicate the bizbaz first.

"""

六 命名规范
总体原则,新编代码必须按下面命名风格进行,现有库的编码尽量保持风格。
1 尽量单独使用小写字母‘l’,大写字母‘O’等容易混淆的字母。
2 模块命名尽量短小,使用全部小写的方式,可以使用下划线。
3 包命名尽量短小,使用全部小写的方式,不可以使用下划线。
4 类的命名使用CapWords的方式,模块内部使用的类采用_CapWords的方式。
5 异常命名使用CapWords+Error后缀的方式。
6 全局变量尽量只在模块内有效,类似C语言中的static。实现方法有两种,一是__all__机制;二是前缀一个下划线。
7 函数命名使用全部小写的方式,可以使用下划线。
8 常量命名使用全部大写的方式,可以使用下划线。
9 类的属性(方法和变量)命名使用全部小写的方式,可以使用下划线。
9 类的属性有3种作用域public、non-public和subclass API,可以理解成C++中的public、private、protected,non-public属性前,前缀一条下划线。
11 类的属性若与关键字名字冲突,后缀一下划线,尽量不要使用缩略等其他方式。
12 为避免与子类属性命名冲突,在类的一些属性前,前缀两条下划线。比如:类Foo中声明__a,访问时,只能通过Foo._Foo__a,避免歧义。如果子类也叫Foo,那就无能为力了。
13 类的方法第一个参数必须是self,而静态方法第一个参数必须是cls。

七 编码建议
1 编码中考虑到其他python实现的效率等问题,比如运算符‘+’在CPython(Python)中效率很高,都是Jython中却非常低,所以应该采用.join()的方式。
2 尽可能使用‘is’‘is not’取代‘==’,比如if x is not None 要优于if x。
3 使用基于类的异常,每个模块或包都有自己的异常类,此异常类继承自Exception。
4 异常中不要使用裸露的except,except后跟具体的exceptions。
5 异常中try的代码尽可能少。比如:

try:
value = collection[key]
except KeyError:
return key_not_found(key)
else:
return handle_value(value)

要优于

try:
# Too broad!
return handle_value(collection[key])
except KeyError:
# Will also catch KeyError raised by handle_value()
return key_not_found(key)

6 使用startswith() and endswith()代替切片进行序列前缀或后缀的检查。比如

Yes: if foo.startswith(‘bar’):优于
No: if foo[:3] == ‘bar’:
7 使用isinstance()比较对象的类型。比如
Yes: if isinstance(obj, int): 优于
No: if type(obj) is type(1):
8 判断序列空或不空,有如下规则
Yes: if not seq:
if seq:
优于
No: if len(seq)
if not len(seq)
9 字符串不要以空格收尾。
10 二进制数据判断使用 if boolvalue的方式。

Guess you like

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