Changeset 2419
- Timestamp:
- Feb 16, 2006, 5:05:12 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
documentation/coding_standards/Style Guide for Python Code, GA version.htm
r2372 r2419 26 26 <title>PEP 8 -- Style Guide for Python Code</title> 27 27 <o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags" 28 name="date"/> 29 <o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags" 28 30 name="country-region"/> 29 31 <o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags" 30 32 name="place"/> 31 <o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"32 name="date"/>33 33 <!--[if gte mso 9]><xml> 34 34 <o:DocumentProperties> 35 35 <o:Author>nielsen ole</o:Author> 36 <o:LastAuthor> nielsen ole</o:LastAuthor>37 <o:Revision> 8</o:Revision>38 <o:TotalTime> 26</o:TotalTime>39 <o:Created>200 5-10-26T07:20:00Z</o:Created>40 <o:LastSaved>200 5-10-26T07:49:00Z</o:LastSaved>36 <o:LastAuthor>sexton jane</o:LastAuthor> 37 <o:Revision>3</o:Revision> 38 <o:TotalTime>45</o:TotalTime> 39 <o:Created>2006-02-16T06:00:00Z</o:Created> 40 <o:LastSaved>2006-02-16T06:02:00Z</o:LastSaved> 41 41 <o:Pages>1</o:Pages> 42 <o:Words> 3898</o:Words>43 <o:Characters>22 219</o:Characters>42 <o:Words>4006</o:Words> 43 <o:Characters>22839</o:Characters> 44 44 <o:Company>Geoscience Australia</o:Company> 45 <o:Lines>1 85</o:Lines>46 <o:Paragraphs>5 2</o:Paragraphs>47 <o:CharactersWithSpaces>26 065</o:CharactersWithSpaces>45 <o:Lines>190</o:Lines> 46 <o:Paragraphs>53</o:Paragraphs> 47 <o:CharactersWithSpaces>26792</o:CharactersWithSpaces> 48 48 <o:Version>10.6626</o:Version> 49 49 </o:DocumentProperties> … … 255 255 to templates. DO NOT USE THIS HTML FILE AS YOUR TEMPLATE! 256 256 --><!--[if gte mso 9]><xml> 257 <o:shapedefaults v:ext="edit" spidmax=" 7170"/>257 <o:shapedefaults v:ext="edit" spidmax="8194"/> 258 258 </xml><![endif]--><!--[if gte mso 9]><xml> 259 259 <o:shapelayout v:ext="edit"> … … 353 353 </td> 354 354 <td style='padding:.75pt .75pt .75pt .75pt'> 355 <p class=MsoNormal><st1:date Month="7" Day="5" Year="2001">05-Jul-2001</st1:date></p>355 <p class=MsoNormal><st1:date Year="2001" Day="5" Month="7">05-Jul-2001</st1:date></p> 356 356 </td> 357 357 </tr> … … 386 386 <pre><span style='mso-spacerun:yes'> </span>This document gives coding conventions for the Python code</pre><pre><span style='mso-spacerun:yes'> </span><span 387 387 class=GramE>comprising</span> the standard library for the main Python distribution.</pre><pre><span style='mso-spacerun:yes'> </span>Please see the companion informational PEP describing style</pre><pre><span style='mso-spacerun:yes'> </span><span 388 class=GramE>guidelines</span> for the C code in the C implementation of Python[1].</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>This document was adapted from Guido's original Python Style</pre><pre><span style='mso-spacerun:yes'> 388 class=GramE>guidelines</span> for the C code in the C implementation of Python[1].</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>This document was adapted from Guido's original Python Style</pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'> </span>Guide <span 389 389 class=GramE>essay[</span>2], with some additions from Barry's style guide[5].</pre><pre><span style='mso-spacerun:yes'> </span>Where there's conflict, Guido's style rules for the purposes of</pre><pre><span style='mso-spacerun:yes'> </span><span 390 390 class=GramE>this</span> PEP.<span style='mso-spacerun:yes'> </span>This PEP may still be incomplete (in fact, it may never</pre><pre><span style='mso-spacerun:yes'> </span><span … … 399 399 class=GramE>the</span> style guide just doesn't apply.<span style='mso-spacerun:yes'> </span>When in doubt, use your best</pre><pre><span style='mso-spacerun:yes'> </span><span 400 400 class=GramE>judgement</span>.<span style='mso-spacerun:yes'> </span>Look at other examples and decide what looks best.</pre><pre><span style='mso-spacerun:yes'> </span>And don't hesitate to ask!</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Two good reasons to break a particular rule:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>(1) When applying the rule would make the code less readable, even</pre><pre><span style='mso-spacerun:yes'> </span><span 401 class=GramE>for</span> someone who is used to reading code that follows the rules.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>(2) To be consistent with surrounding code that also breaks it</pre><pre><span style='mso-spacerun:yes'> 401 class=GramE>for</span> someone who is used to reading code that follows the rules.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>(2) To be consistent with surrounding code that also breaks it</pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'> </span>(<span 402 402 class=GramE>maybe</span> for historic reasons) -- <span class=GramE>although</span> this is also an</pre><pre><span style='mso-spacerun:yes'> </span><span 403 403 class=GramE>opportunity</span> to clean up someone else's mess (in true XP style).</pre><pre><o:p> </o:p></pre><pre><o:p> </o:p></pre> … … 429 429 class=GramE>lines</span> to a maximum of 79 characters (<span class=SpellE>Emacs</span> wraps lines that are</pre><pre><span style='mso-spacerun:yes'> </span><span 430 430 class=GramE>exactly</span> 80 characters long).<span style='mso-spacerun:yes'> </span>For flowing long blocks of text</pre><pre><span style='mso-spacerun:yes'> </span>(<span 431 class=SpellE> <span class=GramE>docstrings</span></span> or comments), limiting the length to 72 characters is</pre><pre><span style='mso-spacerun:yes'> </span><span431 class=SpellE>docstrings</span> or comments), limiting the length to 72 characters is</pre><pre><span style='mso-spacerun:yes'> </span><span 432 432 class=GramE>recommended</span>.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>The preferred way of wrapping long lines is by using Python's</pre><pre><span style='mso-spacerun:yes'> </span><span 433 433 class=GramE>implied</span> line continuation inside parentheses, brackets and braces.</pre><pre><span style='mso-spacerun:yes'> </span>If necessary, you can add an extra pair of parentheses around an</pre><pre><span style='mso-spacerun:yes'> </span><span … … 437 437 class=GramE>class</span> Rectangle(Blob):</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span 438 438 class=GramE>def</span> __init__(self, width, height,</pre><pre><span style='mso-spacerun:yes'> </span><span 439 class=SpellE> <span class=GramE>color</span></span>='black', emphasis=None, highlight=0):</pre><pre><span style='mso-spacerun:yes'> </span><span439 class=SpellE>color</span>='black', emphasis=None, highlight=0):</pre><pre><span style='mso-spacerun:yes'> </span><span 440 440 class=GramE>if</span> width == 0 and height == 0 and \</pre><pre><span style='mso-spacerun:yes'> </span><span 441 class=SpellE> <span class=GramE>color</span></span> == 'red' and emphasis == 'strong' or \</pre><pre><span style='mso-spacerun:yes'> </span><span441 class=SpellE>color</span> == 'red' and emphasis == 'strong' or \</pre><pre><span style='mso-spacerun:yes'> </span><span 442 442 class=GramE>highlight</span> > 100:</pre><pre><span style='mso-spacerun:yes'> </span><span 443 443 class=GramE>raise</span> <span class=SpellE>ValueError</span>("sorry, you lose")</pre><pre><span style='mso-spacerun:yes'> </span><span … … 446 446 class=GramE>raise</span> <span class=SpellE>ValueError</span>("I don't think so")</pre><pre><span style='mso-spacerun:yes'> </span><span 447 447 class=SpellE>Blob.__init</span>_<span class=GramE>_(</span>self, width, height,</pre><pre><span style='mso-spacerun:yes'> </span><span 448 class=SpellE> <span class=GramE>color</span></span>, emphasis, highlight)</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Blank Lines</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Separate top-level function and class definitions with two blank</pre><pre><span style='mso-spacerun:yes'> </span><span448 class=SpellE>color</span>, emphasis, highlight)</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Blank Lines</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Separate top-level function and class definitions with two blank</pre><pre><span style='mso-spacerun:yes'> </span><span 449 449 class=GramE>lines</span>.<span style='mso-spacerun:yes'> </span>Method definitions inside a class are separated by a</pre><pre><span style='mso-spacerun:yes'> </span><span 450 450 class=GramE>single</span> blank line.<span style='mso-spacerun:yes'> </span>Extra blank lines may be used (<s>sparingly</s> <i … … 454 454 class=GramE>implementations</span>).</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>When blank lines are used to separate method definitions, there is</pre><pre><span style='mso-spacerun:yes'> </span><span 455 455 class=GramE>also</span> a blank line between the `class' line and the first method</pre><pre><span style='mso-spacerun:yes'> </span><span 456 class=GramE>definition</span>.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> 456 class=GramE>definition</span>.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'> </span>Use blank lines in functions, <s>sparingly</s> <i 457 457 style='mso-bidi-font-style:normal'><span style='color:blue'>liberally</span></i>, to indicate logical</pre><pre><span style='mso-spacerun:yes'> </span><span 458 458 class=GramE>sections</span>.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Python accepts the control-L (i.e. ^L) form feed character as</pre><pre><span style='mso-spacerun:yes'> </span><span 459 class=SpellE> <span class=GramE>whitespace</span></span>; <span class=SpellE>Emacs</span> (and some printing tools) treat these</pre><pre><span style='mso-spacerun:yes'> </span><span459 class=SpellE>whitespace</span>; <span class=SpellE>Emacs</span> (and some printing tools) treat these</pre><pre><span style='mso-spacerun:yes'> </span><span 460 460 class=GramE>characters</span> as page separators, so you may use them to separate</pre><pre><span style='mso-spacerun:yes'> </span><span 461 461 class=GramE>pages</span> of related sections of your file.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Encodings (<a … … 493 493 class=GramE>standard</span> library imports</pre><pre><span style='mso-spacerun:yes'> </span>2. <span 494 494 class=GramE>related</span> major package imports (i.e. all email package imports next)</pre><pre><span style='mso-spacerun:yes'> </span>3. <span 495 class=GramE>application</span> specific imports</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>You should put a blank line between each group of imports.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'></span>- Relative imports for intra-package imports are highly</pre><pre><span style='mso-spacerun:yes'> </span><span495 class=GramE>application</span> specific imports</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>You should put a blank line between each group of imports.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- Relative imports for intra-package imports are highly</pre><pre><span style='mso-spacerun:yes'> </span><span 496 496 class=GramE>discouraged</span>.<span style='mso-spacerun:yes'> </span>Always use the absolute package path for all</pre><pre><span style='mso-spacerun:yes'> </span><span 497 497 class=GramE>imports</span>. <i style='mso-bidi-font-style:normal'><span … … 529 529 class=GramE>slicing</span>, as in: "<span class=SpellE>dict</span> ['key'] = list [index]".<span style='mso-spacerun:yes'> </span>Always</pre><pre><span style='mso-spacerun:yes'> </span><span 530 530 class=GramE>write</span> this as "<span class=SpellE>dict</span>['key'] = list[index]".</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- More than one space around an assignment (or other) operator to</pre><pre><span style='mso-spacerun:yes'> </span><span 531 class=GramE>align</span> it with another, as in:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>x<span style='mso-spacerun:yes'> </span>= 1</pre><pre><span style='mso-spacerun:yes'> </span>y<span style='mso-spacerun:yes'> </span>= 2</pre><pre> <span style='mso-spacerun:yes'></span><span532 class=SpellE>long_variable</span> = 3</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Always write this as</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> 531 class=GramE>align</span> it with another, as in:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>x<span style='mso-spacerun:yes'> </span>= 1</pre><pre><span style='mso-spacerun:yes'> </span>y<span style='mso-spacerun:yes'> </span>= 2</pre><pre><span style='mso-spacerun:yes'> </span><span 532 class=SpellE>long_variable</span> = 3</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Always write this as</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'> </span>x = 1</pre><pre><span style='mso-spacerun:yes'> </span>y = 2</pre><pre><span style='mso-spacerun:yes'> </span><span 533 533 class=SpellE>long_variable</span> = 3</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>(Don't bother to argue with him on any of the above -- Guido's</pre><pre><span style='mso-spacerun:yes'> </span><span 534 534 class=GramE>grown</span> accustomed to this style over 20 years.)</pre><pre><o:p> </o:p></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Other Recommendations</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- Always surround these binary operators with a single space on</pre><pre><span style='mso-spacerun:yes'> </span><span … … 541 541 class=SpellE>a+b</span>) * (a-b)</pre><pre><span style='mso-spacerun:yes'> </span>c = (a + b) * (a - b)</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- Don't use spaces around the '=' sign when used to indicate a</pre><pre><span style='mso-spacerun:yes'> </span><span 542 542 class=GramE>keyword</span> argument or a default parameter value.<span style='mso-spacerun:yes'> </span>For instance:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span 543 class=GramE>def</span> complex(real, <span class=SpellE>imag</span>=0.0):</pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'></span><span543 class=GramE>def</span> complex(real, <span class=SpellE>imag</span>=0.0):</pre><pre><span style='mso-spacerun:yes'> </span><span 544 544 class=GramE>return</span> magic(r=real, <span class=SpellE>i</span>=<span 545 545 class=SpellE>imag</span>)</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- Compound statements (multiple statements on the same line) are</pre><pre><span style='mso-spacerun:yes'> </span><span … … 548 548 class=GramE>thing</span></span><span class=GramE>()</span></pre><pre><span style='mso-spacerun:yes'> </span>Yes: if <span 549 549 class=SpellE>foo</span> == 'blah':</pre><pre><span style='mso-spacerun:yes'> </span><span 550 class=SpellE>do_blah_<span class=GramE>thing</span></span><span class=GramE>()</span></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'></span>No:<span style='mso-spacerun:yes'> </span><span550 class=SpellE>do_blah_<span class=GramE>thing</span></span><span class=GramE>()</span></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>No:<span style='mso-spacerun:yes'> </span><span 551 551 class=SpellE>do_<span class=GramE>one</span></span><span class=GramE>(</span>); <span 552 552 class=SpellE>do_two</span>(); <span class=SpellE>do_three</span>()</pre><pre><span style='mso-spacerun:yes'> </span>Yes: <span … … 581 581 class=GramE>be</span> separated by at least two spaces from the statement.<span style='mso-spacerun:yes'> </span>They</pre><pre><span style='mso-spacerun:yes'> </span><span 582 582 class=GramE>should</span> start with a # and a single space.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Inline comments are unnecessary and in fact distracting if they state</pre><pre><span style='mso-spacerun:yes'> </span><span 583 class=GramE>the</span> obvious.<span style='mso-spacerun:yes'> </span>Don't do this:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>x = x+1<span style='mso-spacerun:yes'> </span># Increment x</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>But sometimes, this is useful:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>x = x+1<span style='mso-spacerun:yes'> </span># Compensate for border</pre><pre><o:p> </o:p></pre><pre><o:p> </o:p></pre> 583 class=GramE>the</span> obvious.<span style='mso-spacerun:yes'> </span>Don't do this:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>x = x+1<span style='mso-spacerun:yes'> </span># Increment x</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>But sometimes, this is useful:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>x = x+1<span style='mso-spacerun:yes'> </span># Compensate for border</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Developer Comments</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>If software is being created in a multi-developer environment, it is often</pre><pre><span style='mso-spacerun:yes'> </span><span 584 class=GramE>useful</span> to comment the code to indicate those sections which require updating </pre><pre><span style='mso-spacerun:yes'> </span><span 585 class=GramE>or</span> refining. They should start with #FIXME followed by the name of the </pre><pre><span style='mso-spacerun:yes'> </span><span 586 class=GramE>developer</span> in standard brackets. The name should be consistent amongst the</pre><pre><span style='mso-spacerun:yes'> </span><span 587 class=GramE>group</span>; either full initials, first name or surname. For example, if </pre><pre><span style='mso-spacerun:yes'> </span>Duncan S Gray found an issue, he could insert in the code:</pre><pre><o:p> </o:p></pre><pre 588 style='margin-top:0cm;margin-right:12.0pt;margin-bottom:0cm;margin-left:12.0pt; 589 margin-bottom:.0001pt'><span style='mso-spacerun:yes'> </span>#FIXME (DSG): Should check that function returns something sensible and<o:p></o:p></pre><pre> <span style='mso-spacerun:yes'> </span>#<span 590 class=GramE>raise</span> a meaningful exception if it returns None for example</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>This allows other developers to identify areas which require attention with </pre><pre><span style='mso-spacerun:yes'> </span><span 591 class=GramE>the</span> added bonus of knowing who logged the issue.</pre><pre><o:p> </o:p></pre> 584 592 585 593 <h3 style='tab-stops:45.8pt list 54.0pt left 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt'>Documentation … … 605 613 class=SpellE>foobang</span></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Optional <span 606 614 class=SpellE>plotz</span> says to <span class=SpellE>frobnicate</span> the <span 607 class=SpellE>bizbaz</span> first.</pre><pre><span style='mso-spacerun:yes'> 615 class=SpellE>bizbaz</span> first.</pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'> </span>"""</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- For one liner <span 608 616 class=SpellE>docstrings</span>, it's okay to keep the closing """ on</pre><pre><span style='mso-spacerun:yes'> </span><span 609 617 class=GramE>the</span> same line.</pre><pre><o:p> </o:p></pre><pre><o:p> </o:p></pre> … … 636 644 class=GramE>of</span> the bumpy look of its letters[4]).<span style='mso-spacerun:yes'> </span>This is also sometimes known as</pre><pre><span style='mso-spacerun:yes'> </span><span 637 645 class=SpellE><span class=GramE>StudlyCaps</span></span><span class=GramE>.</span></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- <span 638 class=SpellE><span class=GramE>mixedCase</span></span> (differs from <span 639 class=SpellE>CapitalizedWords</span> by initial lowercase</pre><pre><span style='mso-spacerun:yes'> </span><span 646 class=SpellE>mixedCase</span> (differs from <span class=SpellE>CapitalizedWords</span> by initial lowercase</pre><pre><span style='mso-spacerun:yes'> </span><span 640 647 class=GramE>character</span>!)</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- <span 641 648 class=SpellE>Capitalized_Words_With_Underscores</span> (ugly!)</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>There's also the style of using a short unique prefix to group</pre><pre><span style='mso-spacerun:yes'> </span><span 642 class=GramE>related</span> names together.<span style='mso-spacerun:yes'> </span>This is not used much in Python, but it</pre><pre><span style='mso-spacerun:yes'> 649 class=GramE>related</span> names together.<span style='mso-spacerun:yes'> </span>This is not used much in Python, but it</pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'> </span><span 643 650 class=GramE>is</span> mentioned for completeness.<span style='mso-spacerun:yes'> </span>For example, the <span 644 651 class=SpellE><span class=GramE>os.stat</span></span><span class=GramE>()</span></pre><pre><span style='mso-spacerun:yes'> </span><span … … 675 682 class=GramE>uppercase</span> letter oh), or `I' (uppercase letter eye) as single</pre><pre><span style='mso-spacerun:yes'> </span><span 676 683 class=GramE>character</span> variable names.<span style='mso-spacerun:yes'> </span>In some fonts, these characters are</pre><pre><span style='mso-spacerun:yes'> </span><span 677 class=SpellE> <span class=GramE>indistinguisable</span></span> from the numerals one and zero.<span style='mso-spacerun:yes'> </span>When tempted</pre><pre684 class=SpellE>indistinguisable</span> from the numerals one and zero.<span style='mso-spacerun:yes'> </span>When tempted</pre><pre 678 685 style='margin-left:12.0pt'><span style='mso-spacerun:yes'> </span><span 679 686 class=GramE>to</span> use `l' use `L' instead, <i style='mso-bidi-font-style: … … 690 697 class=GramE>(e.g. _socket).</span></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Python packages should have short, all-lowercase names, <s>without</s> </pre><pre><span style='mso-spacerun:yes'> </span><span 691 698 class=GramE><i style='mso-bidi-font-style:normal'><span style='color:blue'>possibly</span></i></span><i 692 style='mso-bidi-font-style:normal'><span style='color:blue'> with</span></i> underscores.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Class Names</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> 699 style='mso-bidi-font-style:normal'><span style='color:blue'> with</span></i> underscores.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Class Names</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'> </span>Almost without exception, class names use the <span 693 700 class=SpellE>CapWords</span> convention.</pre><pre><span style='mso-spacerun:yes'> </span>Classes for internal use have a leading underscore in addition.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Exception Names</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>If a module defines a single exception raised for all sorts of</pre><pre><span style='mso-spacerun:yes'> </span><span 694 701 class=GramE>conditions</span>, it is generally called "error" or "Error".<span style='mso-spacerun:yes'> </span>It seems</pre><pre><span style='mso-spacerun:yes'> </span><span … … 703 710 class=GramE>with</span> an underscore to prevent exporting them.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Function Names</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Function names should be lowercase, possibly with words separated by</pre><pre><span style='mso-spacerun:yes'> </span><span 704 711 class=GramE>underscores</span> to improve readability.<span style='mso-spacerun:yes'> </span><span 705 class=SpellE> <span class=GramE>mixedCase</span></span> is allowed only in</pre><pre><span style='mso-spacerun:yes'> </span><span712 class=SpellE>mixedCase</span> is allowed only in</pre><pre><span style='mso-spacerun:yes'> </span><span 706 713 class=GramE>contexts</span> where that's already the prevailing style (e.g. <span 707 714 class=SpellE>threading.py</span>),</pre><pre><span style='mso-spacerun:yes'> </span><span 708 715 class=GramE>to</span> retain backwards compatibility.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Method Names and Instance Variables</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>The story is largely the same as with functions: in general, use</pre><pre><span style='mso-spacerun:yes'> </span><span 709 716 class=GramE>lowercase</span> with words separated by underscores as necessary to improve</pre><pre><span style='mso-spacerun:yes'> </span><span 710 class=GramE>readability</span>.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Use one leading underscore only for internal methods and instance</pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'></span><span717 class=GramE>readability</span>.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>Use one leading underscore only for internal methods and instance</pre><pre><span style='mso-spacerun:yes'> </span><span 711 718 class=GramE>variables</span> which are not intended to be part of the class's public</pre><pre><span style='mso-spacerun:yes'> </span><span 712 719 class=GramE>interface</span>.<span style='mso-spacerun:yes'> </span>Python does not enforce this; it is up to programmers to</pre><pre><span style='mso-spacerun:yes'> </span><span … … 766 773 class=GramE>domain-specific</span> base exception class, which should be subclassed</pre><pre><span style='mso-spacerun:yes'> </span><span 767 774 class=GramE>from</span> the built-in Exception class.<span style='mso-spacerun:yes'> </span>Always include a class</pre><pre><span style='mso-spacerun:yes'> </span><span 768 class=SpellE> <span class=GramE>docstring</span></span>.<span style='mso-spacerun:yes'> </span>E.g.:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span775 class=SpellE>docstring</span>.<span style='mso-spacerun:yes'> </span>E.g.:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span 769 776 class=GramE>class</span> <span class=SpellE>MessageError</span>(Exception):</pre><pre><span style='mso-spacerun:yes'> </span><span 770 777 class=GramE>"""Base class for errors in the email package."""</span></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>When raising an exception, use "raise <span … … 791 798 class=SpellE>int</span>):</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>When checking if an object is a string, keep in mind that it</pre><pre><span style='mso-spacerun:yes'> </span><span 792 799 class=GramE>might</span> be a <span class=SpellE>unicode</span> string too!<span style='mso-spacerun:yes'> </span>In Python 2.3, <span 793 class=SpellE> <span class=GramE>str</span></span> and <span class=SpellE>unicode</span></pre><pre><span style='mso-spacerun:yes'> </span><span800 class=SpellE>str</span> and <span class=SpellE>unicode</span></pre><pre><span style='mso-spacerun:yes'> </span><span 794 801 class=GramE>have</span> a common base class, <span class=SpellE>basestring</span>, so you can do:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span 795 802 class=GramE>if</span> <span class=SpellE>isinstance</span>(obj, <span … … 799 806 class=GramE>from</span> types import <span class=SpellE>StringTypes</span></pre><pre><span style='mso-spacerun:yes'> </span><span 800 807 class=GramE>if</span> <span class=SpellE>isinstance</span>(obj, <span 801 class=SpellE>StringTypes</span>):</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> 808 class=SpellE>StringTypes</span>):</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'> </span>In Python 2.0 and 2.1, you should do:</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span 802 809 class=GramE>from</span> types import <span class=SpellE>StringType</span>, <span 803 class=SpellE>UnicodeType</span></pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'></span><span810 class=SpellE>UnicodeType</span></pre><pre><span style='mso-spacerun:yes'> </span><span 804 811 class=GramE>if</span> <span class=SpellE>isinstance</span>(obj, <span 805 812 class=SpellE>StringType</span>) or \</pre><pre><span style='mso-spacerun:yes'> </span><span … … 815 822 class=SpellE><span class=GramE>len</span></span><span class=GramE>(</span><span 816 823 class=SpellE>seq</span>) == 0 to check for emptiness.<o:p></o:p></span></i></pre><pre><o:p> </o:p></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- Don't write string literals that rely on significant trailing</pre><pre><span style='mso-spacerun:yes'> </span><span 817 class=SpellE> <span class=GramE>whitespace</span></span>.<span style='mso-spacerun:yes'> </span>Such trailing <span824 class=SpellE>whitespace</span>.<span style='mso-spacerun:yes'> </span>Such trailing <span 818 825 class=SpellE>whitespace</span> is visually</pre><pre><span style='mso-spacerun:yes'> </span><span 819 826 class=GramE>indistinguishable</span> and some editors (or more recently,</pre><pre><span style='mso-spacerun:yes'> </span><span 820 827 class=SpellE>reindent.py</span>) will trim them.</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>- Don't compare <span 821 class=SpellE> <span class=GramE>boolean</span></span> values to True or False using == (<span828 class=SpellE>boolean</span> values to True or False using == (<span 822 829 class=SpellE>bool</span></pre><pre><span style='mso-spacerun:yes'> </span><span 823 830 class=GramE>types</span> are new in Python 2.3):</pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>No:<span style='mso-spacerun:yes'> </span>if greeting == True:</pre><pre><span style='mso-spacerun:yes'> </span>Yes: if greeting:</pre><pre><span … … 831 838 href="http://www.python.org/peps/pep-0007.html">PEP 7</a>, Style Guide for C Code, van <span 832 839 class=SpellE>Rossum</span></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>[2] <a 833 href="http://www.python.org/doc/essays/styleguide.html">http://www.python.org/doc/essays/styleguide.html</a></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span><span style='mso-spacerun:yes'></span>[3] <a840 href="http://www.python.org/doc/essays/styleguide.html">http://www.python.org/doc/essays/styleguide.html</a></pre><pre><o:p> </o:p></pre><pre><span style='mso-spacerun:yes'> </span>[3] <a 834 841 href="http://www.python.org/peps/pep-0257.html">PEP 257</a>, <span 835 842 class=SpellE>Docstring</span> Conventions, <span class=SpellE>Goodger</span>, <span
Note: See TracChangeset
for help on using the changeset viewer.