Python专题――PEP8代码编写规范

上传人:我*** 文档编号:135970315 上传时间:2020-06-21 格式:DOC 页数:20 大小:77.50KB
返回 下载 相关 举报
Python专题――PEP8代码编写规范_第1页
第1页 / 共20页
Python专题――PEP8代码编写规范_第2页
第2页 / 共20页
Python专题――PEP8代码编写规范_第3页
第3页 / 共20页
Python专题――PEP8代码编写规范_第4页
第4页 / 共20页
Python专题――PEP8代码编写规范_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《Python专题――PEP8代码编写规范》由会员分享,可在线阅读,更多相关《Python专题――PEP8代码编写规范(20页珍藏版)》请在金锄头文库上搜索。

1、1. 代码布局设计1.1 缩进A、使用四个空格来进行缩进B、换行的时候可以使用反斜杠,最好的方法是使用圆括号,在使用反斜杠的时候,在反斜杠的后直接回车,不能有任何空格存在。比较好的做法如下:# Aligned with opening delimiter.foo = long_function_name(var_one, var_two, var_three, var_four)包含更多的缩进表示是剩余部分:# More indentation included to distinguish this from the rest.def long_function_name( var_one,

2、 var_two, var_three, var_four): print(var_one) 悬挂缩进应该添加一个级别:# Hanging indents should add a level.foo = long_function_name( var_one, var_two, var_three, var_four)比较差的做法如下:(代码同样是可以运行的)# Arguments on first line forbidden when not using vertical alignment.未使用垂直对齐foo = long_function_name(var_one, var_two

3、, var_three, var_four)# Further indentation required as indentation is not distinguishable.(未使用缩进来表示每一层级)def long_function_name( var_one, var_two, var_three, var_four): print(var_one)对于续行来说,四个空格的缩进是可选的。可选的如下:# Hanging indents *may* be indented to other than 4 spaces.悬挂缩进的时候可以不是四个空格foo = long_functio

4、n_name( var_one, var_two, var_three, var_four)当使用if语句的时候,如果条件恰好的缩进为四个空格空格,那么导致后面的语句的缩进也是四个空格,那么这种情况下是可以接受的,如下所示:没有额外的缩进:# No extra indentation.if (this_is_one_thing and that_is_another_thing): do_something()添加一个注释来进行分割缩进,做到语法高亮显示:# Add a comment, which will provide some distinction in editors# suppo

5、rting syntax highlighting.if (this_is_one_thing and that_is_another_thing): # Since both conditions are true, we can frobnicate. do_something()在续行中添加额外的缩进:# Add some extra indentation on the conditional continuation line.if (this_is_one_thing and that_is_another_thing): do_something()成对的小括号,中括号在多行的结

6、构中可以写成多行,然后括号在第一个不为空白的位置结束。如下:my_list = 1, 2, 3, 4, 5, 6, result = some_function_that_takes_arguments( a, b, c, d, e, f, )或者对齐第一个字符的位置结束,如下:my_list = 1, 2, 3, 4, 5, 6,result = some_function_that_takes_arguments( a, b, c, d, e, f,)1.2 tab和空格的选择关于tab的空格的选择,在python2中是可以混用的,但是在python3中,只能用一种风格。1.3 最大行长度

7、行的最大长度为79个字符在书写文档或者是注释的时候,行长度应该控制在72个字符。反斜杠在有的时候是适用的,例如在参数很长,但是不能隐式的使用多行的时候,如下反斜杠的使用:with open(/path/to/some/file/you/want/to/read) as file_1, open(/path/to/some/file/being/written, w) as file_2: file_2.write(file_1.read()确保在合适的时候将连续的行进行分开,最好的位置是操作符之后,而不是在操作符之前,如下:class Rectangle(Blob): def _init_(s

8、elf, width, height, color=black, emphasis=None, highlight=0): if (width = 0 and height = 0 and color = red and emphasis = strong or highlight 100): raise ValueError(sorry, you lose) if width = 0 and height = 0 and (color = red or emphasis is None): raise ValueError(values are %s, %s % (width, height

9、) Blob._init_(self, width, height, color, emphasis, highlight)1.4 空行l Top level函数和类的定义的时候,空两行。l 类中方法的定义空一行。l 在函数中谨慎使用空行来表示相关的逻辑段。l 无关的函数之间用一个空行进行分割。1.5 源文件编码在源文件中一直使用utf-8编码,在python2中使用ascll编码(存疑?)。文件,在python2 中使用ascll编码,在python3中使用utf-8编码1.6 导入Import经常使用单独的行,如下:import osimport sys或者使用如下的方式:from sub

10、process import Popen, PIPE模块内容顺序:模块说明和docstringimportglobals&constants其他定义import总是在文件的最上行,在模块的注释和docstring之后,在模块的全局变量之前。import可以按照以下顺序进行组织: A标准类库import B第三方import C本地类库import在每个组导入之后,可以用空行进行分割把所有_all_相关类型的声明放在import之后推荐使用绝对导入,可读性强,如下:import mypkg.siblingfrom mypkg import siblingfrom mypkg.sibling im

11、port example对于复杂的封装布局来说,相对导入也是可以接受的,主要是使用绝对导入的时候路径太长,如下:from . import siblingfrom .sibling import example当导入一个类的时候,可以使用如下的方式:from myclass import MyClassfrom foo.bar.yourclass import YourClass当以上的写法导致本地名称冲突,可以写成如下:import myclassimport foo.bar.yourclass并且使用myclass.MyClass andfoo.bar.yourclass.YourClas

12、s。在导入模块的时候,应该避免通配符的存在,如下:from import *2. 字符串引号在对于字符串的标示中,使用双引号还是单引号是没有区别的,主要就是两者混合使用从而避免反斜杠的出现。3. 在表达式和语句中使用空格3.1 避免使用空格情况A 在小括号,中括号,大括号中避免使用空格Yes: spam(ham1, eggs: 2)No: spam( ham 1 , eggs: 2 )B 在逗号,分号,冒号之前不需要空格Yes: if x = 4: print x, y; x, y = y, xNo: if x = 4 : print x , y ; x , y = y , xC 在切片的时候

13、,避免使用空格,在扩展的切片中,必须使用相同的空格个数,如下所示:Yes:ham1:9, ham1:9:3, ham:9:3, ham1:3, ham1:9:hamlower:upper, hamlower:upper:, hamlower:stephamlower+offset : upper+offsetham: upper_fn(x) : step_fn(x), ham: step_fn(x)hamlower + offset : upper + offsetNo:hamlower + offset:upper + offsetham1: 9, ham1 :9, ham1:9 :3hamlower : : upperham : upperD函数的左括号前不要添加空格:Yes: spam(1)No: spam (1)E 中括号前不要添加空格Yes: dctkey = lstindexNo: dct key = lst indexF 操作符左右各一个空格,不要为了追求一致从而添加空格个数Yes:x = 1y = 2long_variable = 3No:x = 1y = 2long_variable = 33.2 其他建议

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 事务文书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号