模块是.py文件python
新建一个.py文件,在里面写上类this
就是一个模块了spa
'''''Created on 2011-11-1 @author: dudong0726 ''' class Person: ''''' classdocs ''' Count = 0 def __init__(self,name,age): ''''' Constructor @param: name the name of this person @param: age the age of this person ''' self.name = name self.age = age Person.Count += 1 def detail(self): ''''' the detail infomation of this person ''' print('name is ',self.name) print('age is ',self.age) print('there are '+str(Person.Count)+" person in the class")
1.怎样引入一个模块?code
from cn import *blog
怎样使用模块中的方法呢?it
'''''Created on 2011-11-1 @author: dudong0726 ''' from cn import * if __name__ == '__main__': p = Person('marry',21) p.detail() q = Person('kevin',24) q.detail()
2.导入模块的第二种方法io
import cn告诉python咱们将要使用这个,当咱们使用时要在前面加上cn.来指明来自cn这个模块class
''' Created on 2011-11-1 @author: dudong0726 ''' import cn if __name__ == '__main__': p = cn.Person('marry',21) p.detail() q = cn.Person('kevin',24) q.detail()
更多详细见http://dudong0726.iteye.com/blog/1226907import