Python · code self-check tool flake8

PEP 8 encoding specification

PEP 8 is a Python code style specification that specifies things like line length, indentation, multi-line expressions, variable naming conventions, and more. Although your team may itself have code style codes that differ slightly from PEP 8, the goal of any code style code is to enforce consistent standards across the codebase, making code more readable and maintainable . The following three libraries can be used to help you beautify your code.

PEP8 Python 编码规范
1代码编排
    1.1缩进。4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格。
    1.2每行最大长度79,换行可以使用反斜杠,最好使用圆括号。换行点要在操作符的后边敲回车。
    1.3类和top-level函数定义之间空两行;类中的方法定义之间空一行;函数内逻辑无关段落之间空一行;其他地方尽量不要再空行。

2文档编排
    2.1模块内容的顺序:模块说明和docstring—import—globals&constants—其他定义。其中import部分,又按标准、三方和自己编写顺序依次排放,之间空一行。
    2.2不要在一句import中多个库,比如import os, sys不推荐。
    2.3如果采用from XX import XX引用库,可以省略‘module.’,都是可能出现命名冲突,这时就要采用import XX。

3空格的使用
总体原则,避免不必要的空格。
    3.1各种右括号前不要加空格。
    3.2逗号、冒号、分号前不要加空格。
    3.3函数的左括号前不要加空格。如Func(1)。
    3.4序列的左括号前不要加空格。如list[2]。
    3.5操作符左右各加一个空格,不要为了对齐增加空格。
    3.6函数默认参数使用的赋值符左右省略空格。
    3.7不要将多句语句写在同一行,尽管使用‘ÿ

おすすめ

転載: blog.csdn.net/qq_37865996/article/details/124362829