Revision 3 as of 2005-02-22 04:34:32

Clear message

Python style conventions are described in PEPs.

TableOfContents()

Learning Python Style

If you're anything like me, you probably can't just read PEP 8 & 257, and start writing perfect Python style.

I've had a lot of success learning (and applying) the document in steps. Start with your line width, spacings. Then work on imports, white space, ... Continue learning and applying the style in steps.

It is probably unrealistic to believe that you can get the whole thing at once.

Examples

I wish there were good examples of well-styled Python code! (Most Python modules do not follow Python style guidelines!)

I intend to write some examples here, and comment on the features of the formatting. Hopefully, people who understand the format better will correct the examples.

-- LionKimbro DateTime(2005-02-19T08:10:35Z)

Example Module

This is my understanding of how a Python module by PEP-8 might look:

   1 """Short description of module.
   2 
   3 Blah blah blah. This is the long description of the module. More brief
   4 explanations.
   5 
   6 ClassNameA --  short description
   7 function_a --  short description
   8 function_b --  short description
   9 """
  10 
  11 import sys  # standard library imports
  12 import os
  13 
  14 import optparse  # related major package imports
  15 
  16 import eggs  # application specific imports
  17 import spam
  18 
  19 
  20 class ClassNameA:
  21 
  22     """What it is.
  23 
  24     Notes on what it is and how it does it.
  25 
  26     Lots to say, lots to say.  I'm unclear on whether we need two spaces
  27     between sentences or not.  72 characters wide.  Blah blah blah.
  28 
  29     Document methods in here, with a short explanation.
  30 
  31     EatEggs --  does X
  32     EatSpam --  does Y
  33     """
  34 
  35     def __init__(self, green_eggs, green_spam):
  36         """Init ClassNameA with some eggs and spam."""
  37         foo()
  38         bar()
  39         baz()
  40 
  41     def EatEggs(self):
  42         """One line documentation is like this."""
  43         foo()
  44         bar()
  45 
  46     def EatSpam(self):
  47         """Short explanation.
  48 
  49         Multi-line spam has a one line description at top, seperated by
  50         a space, and then more multi-line spam afterwards.  Note that
  51         the final triple-quote appears beneath.
  52         """
  53         baz()
  54 
  55 
  56 def function_a(eggs, ham):
  57     """A function_a is a foobar.
  58 
  59     Again, multi-line rules apply in here as well, the same way.  Note
  60     that we have two spaces between module-level class and function
  61     definitions.  That's PEP-8 at work.
  62     """
  63     foo()
  64     bar()
  65     baz()
  66 
  67 
  68 def function_b(eggs, ham):
  69     """A function_b is a foobar.
  70 
  71     Another one.  Still two spaces separating functions and classes.
  72     """
  73     foo()
  74     bar()
  75     baz()

This may be incorrect; please correct it if you know better.

In particular, I'm not sure if there are zero, one, or two spaces between the module docstring, and the import lines.

-- LionKimbro DateTime(2005-02-21T04:44:51Z)

I know these PEPs make no mention of doctest code in docstrings. However, some of them might be nice in this example.

Unable to edit the page? See the FrontPage for instructions.