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中的集中代码注释以及应该注意的编码规范. 代码注释 通过在程序代码中添加注释可提高程序 ...
随机推荐
- MySql最土的语法解释使用一。
create database namedb charset utf8;解释:创建一个数据库 namedb改成你的数据库名字,charset是字符集的意思 utf8代表数据库支持中文字符集.必须分号结 ...
- vue day5 分页控件
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- uva 202
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> # ...
- [翻译][Java]ExecutorService的正确关闭方法
https://blog.csdn.net/zaozi/article/details/38854561 https://blog.csdn.net/z69183787/article/details ...
- 如何清除SQLServer服务器名称、登录名等
SQLServer 2008 R2清理方法: 找到下面路径:C:\Users\%username%\AppData\Roaming\Microsoft\Microsoft SQL Server\100 ...
- c++——智能指针学习(unique_ptr)
1.为什么会有unique_ptr? 动态内存忘记delete,导致内存泄漏.比如: p = new (); if(...) { return ; } delete p; 因此我们需要一种方式来解决这 ...
- JIT(Just in time,即时编译,边运行边编译)、AOT(Ahead Of Time,运行前编译),是两种程序的编译方式
JIT(Just in time,即时编译,边运行边编译).AOT(Ahead Of Time,运行前编译),是两种程序的编译方式
- ubuntu 14.04升级gcc 4.8到5.3
添加PPA库 #sudo apt-get install software-properties-common #sudo add-apt-repository ppa:ubuntu-toolchai ...
- Eclipse中Java build path的使用
1.Eclipse中,工程属性的Java Build Path的Library标签页下,有如下几个按钮:Add Jars...添加JAR包,是指本Eclipse当前包含的工程中的,在工程列表下选取即可 ...
- MS+Oracle各种兼容性的坑
自17年开始新产品开始全面支持Oracle 12c,但陆续发现各种环境问题兼容性的坑,在此汇总一下: 使用11.2.0.1的客户端版本,在连接12c时,发现system账号登陆报用户名密码错误,普通的 ...