py怎样建立模块和怎样导入模块

Posted on 2017-01-10 17:19  上善其若水,厚德载物  阅读(276)  评论(0编辑  收藏  举报

模块是.py文件

新建一个.py文件,在里面写上类

就是一个模块了

'''''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.怎样引入一个模块?

from cn import *

怎样使用模块中的方法呢?

    '''''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.导入模块的第二种方法

import cn告诉python我们将要使用这个,当我们使用时要在前面加上cn.来指明来自cn这个模块

    ''' 
    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/1226907

Copyright © 2024 上善其若水,厚德载物
Powered by .NET 8.0 on Kubernetes