此时被导入模块若定义了__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 #NameError: name 'func1' is not defined zhansan 18 func() is run!
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! 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__中,可以导入 a=_A('zhangsan') # _A在__all__中,可以导入
放入__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!
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!