Python最简编码规范 !

作者: 14e61d025165 | 来源:发表于2019-04-07 15:33 被阅读0次

    本文是阅读《Python Coding Rule》之后总结的最为精华及简单的编码规范,根据每个人不同喜好有些地方会有不同的选择,我只是做了对自己来说最简单易行的选择,仅供大家参考。

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554622349697 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    加群:683380553 获取最下资料!

    1、重要原则

    a.保持风格的一致性很重要,但最重要的是:知道何时不一致

    b.打破一条既定规则的两个好理由:

    c.当应用规则会导致代码可读性下降(可读性赛高)

    d.为了和周围代码保持一致而打破规则(历史遗留)

    2、最简规范

    a.只使用空格缩进

    b.使用UTF-8编码

    c.每行只写一条语句

    d.使用行末反斜杠折叠长行,限制每行最大79字符

    e.导入包:每行唯一、从大到小、绝对路径

    f.类内方法空1行分隔,类外空2行分隔

    g.运算符除 * 外,两边空1格分隔,函数参数=周围不用空格

    h.除类名使用驼峰法以外,其他模块、函数、方法、变量均使用全小写+下划线

    i.1个前导下划线表示半公开,2个前导下划线表示私有,与保留字区分使用单个后置下划线

    j.开发时使用中文注释,发布时再写英文文档

    3、详细规范

    a.全文通用

    b.只使用空格缩进,4个空格表示1个缩进层次

    c.每行长度限制在79字符内,使用行末反斜杠折叠长行

    d.使用UTF-8编码

    e.每行只写一条语句

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554622349702 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    4、代码命名

    一行只import一个包,Imports的顺序为:标准库、相关主包、特定应用,每组导入之间放置1行空行,所有导入使用包的绝对路径。

    分割顶层函数和类的定义使用2行空行,分割类内方法定义使用1行空行,class行与第一个方法定义之间要有1行空行。

    整体使用英文书写方式来使用空格,即仅在逗号、分号后面添加1个空格,其他任何符号如圆括号、方括号、花括号等都不用空格把符号与字符分开,写在一起表示一个整体;运算符除 * 号以外,其他符号两边都各用1个空格分隔;函数参数=号周围不用空格。

    模块名:不含下划线、简短、全小写;

    类名、异常名:首字母大写单词串的驼峰法;

    函数名、全局变量名、方法名、实例变量:全小写,加下划线增加可读性;

    一个前导下划线仅用于不想被导入的全局变量(还有内部函数和类)前加一个下划线)、不打算作为类的公共接口的内部方法和实例变量;

    两个前导下划线以表示类私有的名字,只用来避免与类(为可以子类化所设计)中的属性发生名字冲突。

    私有属性必须有两个前导下划线,无后置下划线;

    非公有属性必须有一个前导下划线,无后置下划线。

    公共属性没有前导和后置下划线,除非它们与保留字冲突,此情况下,单个后置下划线比前置或混乱的拼写要好,例如:class_优于klass。

    5、编写技巧

    与None之类的单值比较,永远用:'is'或'is not'来做:if x is not None

    在模块和包内定义基异常类(base exception class)

    使用字符串方法(methods)代替字符串模块。

    在检查前缀或后缀时避免对字符串进行切片,用startswith()和endswith()代替,如:No: if foo[:3] == 'bar':Yes: if foo.startswith('bar'):

    只用isinstance()进行对象类型的比较,如:No: if type(obj) is type(1):Yes: if isinstance(obj, int)

    判断True或False不要用 ==,如:No: if greeting == True:Yes: if greeting:

    6、注释

    开发时,注释全部用中文来写,当要发布脚本工具时,再写英文文档。

    注释应该是是完整的句子(短语也可),首字母大写;如果注释很短,省略末尾句号;注释块由一个or多个完整句子构成的段落组成,则每个句子使用句子结尾;句末句号后使用两个空格。

    注释块每行以#和一个空格开始,并且跟随注释的代码具有相同的缩进层次,注释块上下方有一空行包围。

    谨慎使用行内注释,至少使用两个空格与语句分开。

    使用 pydoc; epydoc; Doxgen 等文档化工具,为所有公共模块、函数、类和方法边写文档字符串,文档字符串对非公开的方法不是必要的,但你应该有一个描述这个方法做什么的注释,这个注释应该在"def"这行后。

    多行文档字符串结尾的""" 应该单独成行。

    版本注记:定义一个变量version = "Revision: 1.4"

    Stay hungry. Stay foolish.

    相关文章

      网友评论

        本文标题:Python最简编码规范 !

        本文链接:https://www.haomeiwen.com/subject/jfesiqtx.html