002.[python学习]python编码规范pep8学习——PEP8第一部分代码布局
关于PEP8的详细说明可以参考官方原文:http://legacy.python.org/dev/peps/pep-0008/
我参考官方文档及其他文章,摘出相关内容而得此文章,具体参考其他文章见文中最后参考资料处。
当想要让自己所写的代码为更多人使用、交流学习时,不能写出只有机器认识的代码,而是对于人而言具有良好的可读性,此时就需要遵从一个公共的约束来规范自己的代码,那么《Style Guide for Python Code(PEP8)》是个很好的选择。
首先PEP8中声明,有以下有理由忽略特定的规范:
使用规范会降低代码可读性可不遵守;
为了与其他代码保持一致可以不遵守规范(这种情况也许是历史原因);
因为代码早于引进规范并且没有必要理由去修改代码时,可以不遵守规范;
代码需要兼容不支持代码风格建议的旧版本python时可不遵守。
1 代码布局
1.1 缩进
每个缩进使用4个空格。
python是靠缩进区分语句块的,语句块缩进不正确是无法通过语法检查的,这种是必须遵守的;
跨行的语句需要遵守下面规范(英语不行,暂时就不翻译了,意会下):
Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a hanging indent [7]. When using a hanging indent the following should be considered; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.
指的是续行应该和括起来的内容垂直对齐,或者使用叫hanging indent的格式。
下面是符合规范的例子:
# 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, 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)
# Hanging indents *may* be indented to other than 4 spaces.
foo = long_function_name(
var_one, var_two,
var_three, var_four)
# 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
# supporting 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()
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',
)
下面是不符合规范的例子:
# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
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)
1.2 使用tab还是空格
空格是更好的选择,尽量不要空格和tab混用。Python3中不允许混用的方式,python2中可以使用-t或-tt参数进行检查,混用时前者会产生警告,后者会产生错误。
1.3 单行最大长度79个字符
1.4 空白行:顶级函数和类定义之间使用两个空白行,类中函数定义之间使用单个空白行。
1.5 源文件编码格式:官方说python3用UTF-8,python2用ASCII,我觉得最好都用UTF-8。
1.6 模块导入应该单号导入一个,除from xxx import a, b这种外;导入语句应该放在源文件最前面,紧跟着模块注释和文档描述之后,在模块全局变量和常量之前。模块应按照顺序导入:最先导入系统标准模块,再导入相关第三方模块,最后导入本地自己编写的模块。
002.[python学习]python编码规范pep8学习——PEP8第一部分代码布局的更多相关文章
- 学习笔记之Python最简编码规范
Python最简编码规范 - 机器学习算法与Python学习 https://mp.weixin.qq.com/s/i6MwvC4jYTE6D1KHFgBeoQ https://www.cnblogs ...
- python的统一编码规范
请注意这一点:没有编码规范的代码没有阅读价值,也更谈不上复用. 目前业界比较流行的Python的编码规范目前主要有PEP8的编程.Google的编码风格.Python Guide和Pocoo Styl ...
- python 编码规范起源:PEP8 编码规范中文版
PEP: 8 标题: Python代码的样式指南 版: c451868df657 最后修改: 2016-06-08 10:43:53 -0400(2016年6月8日星期三) 作者: Guido van ...
- Python PEP 8 编码规范中文版
原文链接:http://legacy.python.org/dev/peps/pep-0008/ 转发链接:https://blog.csdn.net/ratsniper/article/detail ...
- Python最简编码规范
前言 本文是阅读<Python Coding Rule>之后总结的最为精华及简单的编码规范,根据每个人不同喜好有些地方会有不同的选择,我只是做了对自己来说最简单易行的选择,仅供大家参考. ...
- PHP编码规范建议学习
###php编码规范 -------* sql过长 ```$sql = <<<SQLSELECT delivery_idFROM d_testWHERE delivery_idIN ...
- Python基础:编码规范(4)
1.命名规范 Python中不同代码元素采用不同命名方式: ◊ 包名:全部小写字母,中间可以由点分隔开.作为命名空间,包名需具有唯一性. ◊ 模块名:全部小写字母,如果是多个单词构成,使用下划线分隔. ...
- [python]python官方原版编码规范路径
1.进入python官方主页:https://www.python.org/ 2.按如下图进入PEP Index 3.选择第8个,即为python的规范
- IT兄弟连 Java语法教程 注释与编码规范
在程序代码中适当地添加注释可以提高程序的可读性和可维护性.好的编码规范可以使程序更易阅读和理解.下面将介绍Java中的集中代码注释以及应该注意的编码规范. 代码注释 通过在程序代码中添加注释可提高程序 ...
随机推荐
- 枚举转map
import java.util.HashMap; import java.util.Map; public enum PayType { ALIPAY("支付宝扫码", 15), ...
- 可持久化trie学习笔记
其实很早之前就想学习可持久化trie,不过由于换队友等情况,还是优先去学数论和计算几何,今天突然心血来潮学了一发可持久化trie,感觉还是蛮简单的,不过由于自己很长时间没写过可持久化了,都快忘了是个什 ...
- 【Jest】笔记二:Matchers匹配器
一.前言 什么是匹配器? 我们可以把匹配器看成,testng断言,这么理解就可以了 二.常用的匹配器 test('two plus two is four', () => { expect(2 ...
- bond模式
1.mode=0(balance-rr)(平衡抡循环策略) 链路负载均衡,增加带宽,支持容错,一条链路故障会自动切换正常链路.交换机需要配置聚合口,思科叫port channel.特点:传输数据包顺序 ...
- C# 生成海报,文本区域指定和换行,图片合成
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string path = Server.MapPa ...
- zookeeper启动报错处理记录
zookeeper启动时正常,查看状态时出错 处理,通过 ./zkServer.sh status 报错Error contacting service. It is probably not run ...
- Thread.Abort 方法
[SecurityPermissionAttribute(SecurityAction.Demand, ControlThread = true)] public void Abort() 在调用此方 ...
- linux与C内存管理机制
转自知乎专栏:https://zhuanlan.zhihu.com/p/51855842?utm_source=wechat_session&utm_medium=social&utm ...
- 【linux】常用命令之用户管理
查看用户 cat /etc/passwd 查看某一个用户 cat /etc/passwd|grep javaluna 新增用户 useradd xxx 更新密码 passwd xxx 删除用户及相关配 ...
- reshape的两个函数melt和dcast
reshape Reshape包主要是用来做数据变形的.其中主要的有两个函数melt和dcast1.其中melt主要用于宽变长,而dcast1主要用于长变宽.melt和dcast1是reshape2包 ...