Python 编码规范(Style Guide)1
2018-06-17 本文已影响3人
LabVIEW_Python
在编写Python代码的时候,最好遵循Python 编码规范,这样,别人容易读懂你的代码,你也容易读懂别人的代码。
1, 缩进:
函数参数缩进,推荐:

条件语句缩进,推荐:

2,将所有行限制为最多79个字符
3,在二元操作符之前断行。Donald Knuth explains the traditional rule in his Computers and Typesetting series: "Although formulas within a paragraph always break after binary operations and relations, displayed formulas always break before binary operations"

4,源文件编码:UTF-8。Python3.X 源码文件默认使用utf-8编码,所以可以正常解析中文,无需指定 UTF-8 编码。若Python2(默认ASCII编码),需要在源文件开头加入 # -*- coding: UTF-8 -*- 或者 #coding=utf-8
5,import语句顺序,import语句按下列顺序排列:
首先是:Standard library imports.
然后是:Related third party imports.
最后是,Local application/library specific imports.
每组import语句之间用空白行分开
6,表达式和语句中的空格,Yes代表推荐风格,No代表不推荐风格

7,尾随逗号(Trailing Comma),通常可以不用, 仅仅在只有一个元素的元组(tuple)里必须使用。若要使用,请遵循下面的风格:
