python中模块的__all__详细使用


python模块中的all,用于模块导入时限制,如: from module import *

此时被导入模块若定义了all属性,则只有all内指定的属性、方法、类可被导入;若没定义,则导入模块内的所有公有属性,方法和类。

1.实例1

#bb.py  
class A():  
    def __init__(self,name,age):  
        self.name=name  
        self.age=age  
class B():  
    def __init__(self,name,id):  
        self.name=name  
        self.id=id  
def fun():  
    print "func() is run!"  
def fun1():  
    print "func1() is run!"  





#test_bb.py  
from bb import *  
a=A('zhansan','18')  
print a.name,a.age  
b=B("lisi",1001)  
print b.name,b.id  
fun()  
fun1()  

运行结果:  
zhansan 18  
lisi 1001  
func() is run!  
func1() is run!  

注:  
  由于bb.py中没有定义__all__属性,所以导入了bb.py中所有的公有属性  


2.实例2  



#bb.py  
__all__=('A','func')  
class A():  
    def __init__(self,name,age):  
        self.name=name  
        self.age=age  
class B():  
    def __init__(self,name,id):  
        self.name=name  
        self.id=id  
def func():  
    print "func() is run!"  
def func1():  
    print "func1() is run!"


#test_bb.py  
from bb import *  
a=A('zhansan','18')  
print a.name,a.age  
func()  
#b=B("lisi",1001)  
#NameError: name 'B' is not defined  
#func1()  
#NameError: name 'func1' is not defined  


运行结果:  
zhansan 18  
func() is run!  

注:  
  由于bb.py中使用了__all__=('A','func'),所以在别的模块导入该模块时,只能导入__all__中的变量、方法、类  


3.实例3  



#bb.py  
def func(): #模块中的public方法  
    print 'func() is run!'  
def _func(): #模块中的protected方法  
    print '_func() is run!'  
def __func(): #模块中的private方法   
    print '__func() is run!'  




#test_bb.py  
from bb import *  #此方式只能导入公有的属性、方法、类【无法导入以单下划线开头(protected)或以双下划线开头(private)的属性、方法、类】  
func()  
#_func()  
#__func()  


运行结果:  
func() is run!  

注:  
  from bb import * --此方式只能导入公有的属性、方法、类【无法导入以单下划线开头(protected)或以双下划线开头(private)的属性、方法、类】  
  _func() #NameError: name '_func' is not defined  
  __func() #NameError: name '__func' is not defined  





4.实例4


#bb.py  
__all__=('func','__func','_A')#放入__all__中所有属性均可导入,即使是以下划线开头  
class _A():  
    def __init__(self,name):  
        self.name=name  
def func():  
    print "func() is run!"  
def func1():  
    print "func1() is run!"  
def _func():  
    print "_func() is run!"  
def __func():  
    print "__func() is run!"  





#test_bb.py  
from bb import *  
func()  
#func1()#func1不在__all__中,无法导入 NameError: name 'func1' is not defined   
#_func()#_func不在__all__中,无法导入 NameError: name '_func' is not defined  
__func()#__func在__all__中,可以导入  
a=_A('zhangsan')#_A在__all__中,可以导入  
print a.name  


运行结果:  
func() is run!  
__func() is run!  
zhangsan  

注:  
  放入__all__中所有属性均可导入,即使是以下划线开头  
  func1() #func1不在__all__中,无法导入 NameError: name 'func1' is not defined  
  _func() #_func不在__all__中,无法导入 NameError: name '_func' is not defined  
  __func() #__func在__all__中,可以导入  
  a=_A('python') #_A在__all__中,可以导入  



5.实例5  




#bb.py  
def func():  
    print 'func() is run!'  
def _func():  
    print '_func() is run!'  
def __func():  
    print '__func() is run!'  





#test_bb.py  
from bb import func,_func,__func #可以通过这种方式导入public,protected,private  
func()  
_func()  
__func()  


运行结果:  
func() is run!  
_func() is run!  
__func() is run!  

注:  
  虽然_func()、__func()属于"protected , private"权限的,但是如果使用该方式,是可以直接导入访问的   


6.实例6  




#bb.py  
def func():  
    print 'func() is run!'  
def _func():  
    print '_func() is run!'  
def __func():  
    print '__func() is run!'


 


#test_bb.py  
import bb #可以通过这种方式导入public,protected,private  
bb.func()  
bb._func()  
bb.__func()  

运行结果:  
func() is run!  
_func() is run!  
__func() is run!  

注:  
  可以通过import模块的方式导入模块,然后使用模块.XX的方式访问"public,protected,private"权限的内容


原文链接:https://www.cnblogs.com/wxlog/p/10566628.html