资源预览内容
第1页 / 共21页
第2页 / 共21页
第3页 / 共21页
第4页 / 共21页
第5页 / 共21页
第6页 / 共21页
第7页 / 共21页
第8页 / 共21页
第9页 / 共21页
第10页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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-Type: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 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 code 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 important. 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 best. 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) - although 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 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 continuation 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 using 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_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 Python 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 make 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 disrupts 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 using 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. The 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 %
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号