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"權限的內容



 
 
 
 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM