python代码规范[英]

上传人:jiups****uk12 文档编号:40008964 上传时间:2018-05-22 格式:DOCX 页数:21 大小:58.06KB
返回 下载 相关 举报
python代码规范[英]_第1页
第1页 / 共21页
python代码规范[英]_第2页
第2页 / 共21页
python代码规范[英]_第3页
第3页 / 共21页
python代码规范[英]_第4页
第4页 / 共21页
python代码规范[英]_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《python代码规范[英]》由会员分享,可在线阅读,更多相关《python代码规范[英](21页珍藏版)》请在金锄头文库上搜索。

1、 PEP Index PEP 8 - Style Guide for Python Code PEP:PEP: 8 Title:Title: Style Guide for Python Code Version:Version: 89db18c77152 Last-Modified:Last-Modified: 2013-01-13 11:28:10 +0100 (Sun, 13 Jan 2013)Author:Author:Guido van Rossum , Barry Warsaw Status:Status: Active Type:Type: Process Content-Typ

2、e:Content-Type: text/x-rst Created:Created: 05-Jul-2001 Post-History:Post-History: 05-Jul-2001IntroductionThis document gives coding conventions for the Python code comprising the standard library in the main Python distribution. Please see the companion informational PEP describing style guidelines

3、 for the C code in the C implementation of Python 1.This document and PEP 257 (Docstring Conventions) were adapted from Guidos original Python Style Guide essay, with some additions from Barrys style guide 2.A Foolish Consistency is the Hobgoblin of Little MindsOne of Guidos key insights is that cod

4、e is read much more often than it is written. The guidelines provided here are intended to improve the readability of code and make it consistent across the wide spectrum of Python code. As PEP 20 says, “Readability counts“.A style guide is about consistency. Consistency with this style guide is imp

5、ortant. Consistency within a project is more important. Consistency within one module or function is most important.But most importantly: know when to be inconsistent - sometimes the style guide just doesnt apply. When in doubt, use your best judgment. Look at other examples and decide what looks be

6、st. And dont hesitate to ask!Two good reasons to break a particular rule:1.When applying the rule would make the code less readable, even for someone who is used to reading code that follows the rules. 2.To be consistent with surrounding code that also breaks it (maybe for historic reasons) - althou

7、gh this is also an opportunity to clean up someone elses mess (in true XP style).Code lay-outIndentationUse 4 spaces per indentation level.For really old code that you dont want to mess up, you can continue to use 8- space tabs.Continuation lines should align wrapped elements either vertically using

8、 Pythons implicit line joining inside parentheses, brackets and braces, or using a hanging indent. When using a hanging indent the following considerations should be applied; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a cont

9、inuation line.Yes:# Aligned with opening delimiterfoo = 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)No:# Arguments on first line forbidden when not usi

10、ng vertical alignmentfoo = long_function_name(var_one, var_two,var_three, var_four)# Further indentation required as indentation is not distinguishabledef long_function_name(var_one, var_two, var_three,var_four):print(var_one)Optional:# Extra indentation is not necessary.foo = long_function_name(var

11、_one, var_two,var_three, var_four)Tabs or Spaces?Never mix tabs and spaces.The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the P

12、ython command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!For new projects, spaces-only are strongly recommended over tabs. Most editors have features that m

13、ake this easy to do.Maximum Line LengthLimit all lines to a maximum of 79 characters.There are still many devices around that are limited to 80 character lines; plus, limiting windows to 80 characters makes it possible to have several windows side-by-side. The default wrapping on such devices disrup

14、ts the visual structure of the code, making it more difficult to understand. Therefore, please limit all lines to a maximum of 79 characters. For flowing long blocks of text (docstrings or comments), limiting the length to 72 characters is recommended.The preferred way of wrapping long lines is by u

15、sing Pythons implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. Th

16、e preferred place to break around a binary operator is after the operator, not before it. Some examples:class Rectangle(Blob):def _init_(self, width, height,color=black, emphasis=None, highlight=0):if (width = 0 and height = 0 andcolor = red and emphasis = strong orhighlight 100):raise ValueError(“sorry, you lose“)if width = 0 and height = 0 and (color = red oremphasis is None):raise ValueError(“I dont think so - values are %

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

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