现在的位置: 首页 > 综合 > 正文

python模块规定的格式,按照这样写,最规范

2012年12月06日 ⁄ 综合 ⁄ 共 742字 ⁄ 字号 评论关闭

python模块规定的格式,按照这样写,最规范。

一个python模块代码结构一般按照以下格式写,请参照!

#1)起始行

#!/usr/bin/env python

# -*- codeing:utf-8 -*-

#2)模块文档
"""Show off features of [pydoc] module

This is a silly module to
demonstrate docstrings
"""

#3)模块信息
__author__ = 'python' 
__version__= '1.0'
__nonsense__ = 'aaaabbbbccc'

 

 

#4)导入模块

import sys

import os

#5)全局变量

 

debug=true

 

#6)类的定义

class MyClass(object): 
  """Demonstrate class docstrings"""
  def __init__ (self, spam=1, eggs=2):
      """Set default attribute values only
      Keyword arguments:
      spam ― a processed meat product
      eggs ― a fine breakfast for lumberjacks
      """
      self.spam = spam
      self.eggs = eggs
      if debug:
          print 'ran __init__(...)'

#7)函数定义

def test():

   "test function"

    myclass = MyClass()

if debug:
             print 'ran test(...)'

#8)主程序定义

     if __name__ == '__main__':

                test()

抱歉!评论已关闭.