Python开发编码规范

-- dreamingk [2004-08-09 01:10:29]

Python开发编码规范

用Python进行开发时的编码风格约定

介绍(Introduction)

愚蠢得使用一致性是无知的妖怪(A Foolish Consistency is the Hobgoblin of Little Minds)

{{{呆板的坚持一致性是傻的没边了! -- Zoomq}}}

代码的布局(Code lay-out)

缩进(Indentation)

制表符还是空格(Tabs or Spaces)?

行的最大长度(Maximum Line Length)

   1     class Rectangle(Blob):
   2 
   3         def __init__(self, width, height,
   4                      color='black', emphasis=None, highlight=0):
   5             if width == 0 and height == 0 and \
               color == 'red' and emphasis == 'strong' or \
               highlight > 100:
   6                 raise ValueError, "sorry, you lose"
   7             if width == 0 and height == 0 and (color == 'red' or
   8                                                emphasis is None):
   9                 raise ValueError, "I don't think so"
  10             Blob.__init__(self, width, height,
  11                           color, emphasis, highlight)

空行(Blank Lines)

编码(Encodings)(PEP 263)

导入(Imports)

        No:  import sys, os
        Yes: import sys
             import os

        from types import StringType, ListType

        from MyClass import MyClass
        from foo.bar.YourClass import YourClass

        import MyClass
       import foo.bar.YourClass

表达式和语句中的空格(Whitespace in Expressions and Statements)

   1           x             = 1
   2           y             = 2
   3           long_variable = 3

   1          x = 1
   2          y = 2
   3          long_variable = 3

其它建议(Other Recommendations)

   1           i = i+1
   2           submitted = submitted + 1
   3           x = x*2 - 1
   4           hypot2 = x*x + y*y
   5           c = (a+b) * (a-b)
   6           c = (a + b) * (a - b)

   1           def complex(real, imag=0.0):
   2               return magic(r=real, i=imag)

          No:  if foo == 'blah': do_blah_thing()
          Yes: if foo == 'blah':
                   do_blah_thing()

          No:  do_one(); do_two(); do_three()
          Yes: do_one()
               do_two()
               do_three()

注释(Comments)

注释块(Block Comments)

行内注释(Inline Comments)

        x = x+1                 # Increment x

        x = x+1                 # Increment x

        x = x+1                 # Compensate for border

        x = x+1                 # Compensate for border

文档字符串(Documentation Strings)

{{{Documentation Strings-- 文档化字符 ; 为了pydoc;epydoc,Doxygen等等文档化工具的使用,类似于MoinMoin 语法,约定一些字符, 以便自动提取转化为有意义的文档章节等等文章元素! -- Zoomq}}}

      """Return a foobang

      Optional plotz says to frobnicate the bizbaz first.
      """

版本注记(Version Bookkeeping)

(我觉得叫"注记"更好)

   1         __version__ = "$Revision: 1.4 $"
   2         # $Source: E:/cvsroot/python_doc/pep8.txt,v $

Python开发编码规范 (last edited 2009-12-25 07:12:40 by localhost)