python 进阶(4) -- PEP8 规范

[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]你让我知道,触摸到头顶的天空是什么感觉[HTML_REMOVED][HTML_REMOVED][HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]前序[HTML_REMOVED] [HTML_REMOVED]学了 python 也有一段时间了,现在在用着 python3,时间久了,也应该慢慢地精进一下,这里从 PEP8 的[HTML_REMOVED]官网[HTML_REMOVED]翻译整理一下,日后慢慢规范,当然也不能生搬硬套,一个标准而已[HTML_REMOVED] [HTML_REMOVED]总的归结为如下几个方面: - 每个缩进层级使用 4 个空格 - 每行最多 79 个字符 - 采用 ASCII 或 UTF-8 编码文件 - 在文件顶端,注释和文档说明之下,每行每条 import 语句只导入一个模块,同时要按标准库、第三方库和本地库的导入顺序进行分组 - 在大中小之间或者逗号之前没有额外的空格 - 类的命名采用骆驼命名法(CameCase); 异常的定义使用 Error 前缀;函数的命名使用小写字符(separated_by_underscored)[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]缩进(Indentation)[HTML_REMOVED] [HTML_REMOVED]最基本的:使用 4 个空格完成一个缩进,一个缩进为一个缩进等级(indent level)[HTML_REMOVED] [HTML_REMOVED]当存在换行情况时,遵循下面缩进规范:[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]当第一行有参数时,对齐界定符排列(比如括号)[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]# Aligned with opening delimiter. foo = long_function_name(var_one, var_two, var_three, var_four) [HTML_REMOVED][HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED] [HTML_REMOVED]Tabs or Spaces?[HTML_REMOVED] [HTML_REMOVED]在 py3 中只能使用同一种风格,这个之后再解决,在复制黏贴的时候经常报错,还不知道是不是这个原因而且该怎样解决[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]最大行长度[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]所有的行最大长度为 79 个字符,所以需要在适当的时候进行换行处理[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]断行在操作符之前[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]income = (gross_wages + taxable_interest + (dividends - qualified_dividends) - ira_deduction - student_loan_interest) [HTML_REMOVED][HTML_REMOVED]

[HTML_REMOVED] [HTML_REMOVED]Black Lines[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]最顶层的函数或类之间隔两个空行[HTML_REMOVED] [HTML_REMOVED]定义在类里面的方法之间隔一个空行[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]Imports[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]同一行导入同一个库函数[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]import[HTML_REMOVED] [HTML_REMOVED]os[HTML_REMOVED] [HTML_REMOVED]import[HTML_REMOVED] [HTML_REMOVED]sys[HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED] [HTML_REMOVED]空格[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED]在大中小括号之间不要有空格[HTML_REMOVED] [HTML_REMOVED] [HTML_REMOVED][HTML_REMOVED][HTML_REMOVED][HTML_REMOVED]spam(ham[1], {eggs: 2}) [HTML_REMOVED][HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED]

[HTML_REMOVED] [HTML_REMOVED]后续[HTML_REMOVED] [HTML_REMOVED]暂时就整理这些,之后需要或者碰到新的再继续更新[HTML_REMOVED] [HTML_REMOVED]