1. 程式人生 > >python模組和類在import上的區別

python模組和類在import上的區別

轉自:http://dudong0726.iteye.com/blog/1226907

1、類屬於模組的一部分。當我們要建立一個類時,通常我們新建一個py檔案,例如新建立cn.py,這個cn便成為我們的模組。

2、然後在cn裡面建立自己的類:

Python程式碼  收藏程式碼
  1. '''''Created on 2011-11-1 
  2. @author: dudong0726 
  3. '''  
  4. class Person:  
  5.     ''''' 
  6.     classdocs 
  7.     '''  
  8.     Count = 0  
  9.     def __init__(self,name,age):  
  10.         ''
    ''' 
  11.         Constructor 
  12.         @param: name the name of this person 
  13.         @param: age the age of this person   
  14.         '''  
  15.         self.name = name  
  16.         self.age = age  
  17.         Person.Count += 1  
  18.     def detail(self):  
  19.         ''''' 
  20.          the detail infomation of this person 
  21.         '''
      
  22.         print('name is ',self.name)  
  23.         print('age is ',self.age)  
  24.         print('there are '+str(Person.Count)+" person in the class")   

3、我們需要在另一個模組中使用這個類,有兩種匯入方式

     1)from cn import * 也就是從cn模組中把所有的東西都匯入進來

Python程式碼  收藏程式碼
  1. '''''Created on 2011-11-1 
  2. @author: dudong0726 
  3. '''  
  4. from cn import
     *  
  5. if __name__ == '__main__':  
  6.     p = Person('marry',21)  
  7.     p.detail()  
  8.     q = Person('kevin',24)  
  9.     q.detail()  

  2)import cn 告訴python我們將要使用這個模組的東西,當我們使用時要在前面加上cn.來指明來自cn這個模組

Python程式碼  收藏程式碼
  1. ''''' 
  2. Created on 2011-11-1 
  3. @author: dudong0726 
  4. '''  
  5. import cn  
  6. if __name__ == '__main__':  
  7.     p = cn.Person('marry',21)  
  8.     p.detail()  
  9.     q = cn.Person('kevin',24)  
  10.     q.detail()  

4、我們可以在cn模組中建立一個函式

Python程式碼  收藏程式碼
  1. ''''' 
  2. Created on 2011-11-1 
  3. @author: dudong0726 
  4. '''  
  5. def say(word):  
  6.     print(word)  
  7. class Person:  
  8.     ''''' 
  9.     classdocs 
  10.     '''  
  11.     Count = 0  
  12.     def __init__(self,name,age):  
  13.         ''''' 
  14.         Constructor 
  15.         @param: name the name of this person 
  16.         @param: age the age of this person   
  17.         '''  
  18.         self.name = name  
  19.         self.age = age  
  20.         Person.Count += 1  
  21.     def detail(self):  
  22.         ''''' 
  23.          the detail infomation of this person 
  24.         '''  
  25.         print('name is ',self.name)  
  26.         print('age is ',self.age)  
  27.         print('there are '+str(Person.Count)+" person in the class")   

5、在另外的模組中呼叫這個函式

  你可以這樣呼叫:

Python程式碼  收藏程式碼
  1. ''''' 
  2. Created on 2011-11-1 
  3. @author: dudong0726 
  4. '''  
  5. from cn import *  
  6. if __name__ == '__main__':  
  7.     p = Person('marry',21)  
  8.     p.detail()  
  9.     q = Person('kevin',24)  
  10.     q.detail()  
  11.     say("hello world")  

    當然也可以這樣:

Python程式碼  收藏程式碼
  1. ''''' 
  2. Created on 2011-11-1 
  3. @author: dudong0726 
  4. '''  
  5. import cn  
  6. if __name__ == '__main__':  
  7.     p = cn.Person('marry',21)  
  8.     p.detail()  
  9.     q = cn.Person('kevin',24)  
  10.     q.detail()  
  11.     cn.say("hello world")