Python 代码模块八股文
- Path and unicode
- Module docstr
- __author__; __version__
- import
- Global Variables
- Class
- Function
- __name__=="__main__"
PDA IV CFM
一个python module代码结构一般按照以下格式写,请参照!
#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'
#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()
No comments:
Post a Comment