python模块和类在import上的区别

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

1、类属于模块的一部分。当我们要建立一个类时,通常我们新建一个py文件,例如新建立cn.py,这个cn便成为我们的模块。

2、然后在cn里面建立自己的类:

Python代码   收藏代码
  1. '''''Created on 2011-11-1 
  2.  
  3. @author: dudong0726 
  4. '''  
  5.   
  6. class Person:  
  7.     ''''' 
  8.     classdocs 
  9.     '''  
  10.     Count = 0  
  11.   
  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.           
  22.     def detail(self):  
  23.         ''''' 
  24.          the detail infomation of this person 
  25.         '''  
  26.         print('name is ',self.name)  
  27.         print('age is ',self.age)  
  28.         print('there are '+str(Person.Count)+" person in the class")   
  29.           

3、我们需要在另一个模块中使用这个类,有两种导入方式

     1)from cn import * 也就是从cn模块中把所有的东西都导入进来

 

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

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

 

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

4、我们可以在cn模块中建立一个函数

 

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

5、在另外的模块中调用这个函数

  你可以这样调用:

 

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

 

    当然也可以这样:

Python代码   收藏代码
  1. ''''' 
  2. Created on 2011-11-1 
  3.  
  4. @author: dudong0726 
  5. '''  
  6. import cn  
  7.   
  8. if __name__ == '__main__':  
  9.     p = cn.Person('marry',21)  
  10.     p.detail()  
  11.     q = cn.Person('kevin',24)  
  12.     q.detail()  
  13.       
  14.     cn.say("hello world")  
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值