py怎樣創建模塊和怎樣導入模塊

模塊是.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

相關文章
相關標籤/搜索