Python 风格指南

一个典型模块的内部结构:

  1. 起始行
  2. 模块文档(文档字符串)
  3. 模块导入
  4. (全局)变量定义
  5. 类定义(若有)
  6. 函数定义(若有)
  7. 主程序

样例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
#coding:utf-8
 
"this is a test module"
 
import sys
import os
 
debug = True
 
class FooClass(object):
    "Foo class"
    pass
 
def test():
    "test function"
    foo = FooClass()
    if debug:
        print 'ram test()'
 
if __name__ == '__main__':
    test()