欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中模塊的__all__屬性詳解

 更新時(shí)間:2017年10月26日 15:40:13   作者:快遞小可  
這篇文章主要介紹了python中模塊的__all__屬性詳解,具有一定參考價(jià)值,需要的朋友可以了解下。

python模塊中的__all__屬性,可用于模塊導(dǎo)入時(shí)限制,如:

from module import *

此時(shí)被導(dǎo)入模塊若定義了__all__屬性,則只有__all__內(nèi)指定的屬性、方法、類可被導(dǎo)入。

若沒(méi)定義,則導(dǎo)入模塊內(nèi)的所有公有屬性,方法和類

# kk.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 func(): 
  print 'func() is called!' 
def func1(): 
  print 'func1() is called!' 
#test_kk.py 
from kk import * #由于kk.py中沒(méi)有定義__all__屬性,所以導(dǎo)入了kk.py中所有的公有屬性、方法、類 
a=A('python','24') 
print a.name,a.age 
b=B('python',123456) 
print b.name,b.id 
func() 
func1() 

運(yùn)行結(jié)果:

python 24
python 123456
func() is called!
func1() is called!

#kk.py 
__all__=('A','func') #在別的模塊中,導(dǎo)入該模塊時(shí),只能導(dǎo)入__all__中的變量,方法和類 
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 called!' 
def func1(): 
  print 'func1() is called!' 
#test_kk.py 
from kk import * #kk.py中定義了__all__屬性,只能導(dǎo)入__all__中定義的屬性,方法和類 
a=A('python','24') 
print a.name,a.age 
func() 
#func1() #NameError: name 'func1' is not defined 
#b=B('python',123456) #NameError: name 'B' is not defined 

運(yùn)行結(jié)果:

python 24
func() is called!

#kk.py 
def func(): #模塊中的public方法 
  print 'func() is called!' 
def _func(): #模塊中的protected方法 
  print '_func() is called!' 
def __func():#模塊中的private方法 
  print '__func() is called!' 
#test_kk.py 
from kk import * #這種方式只能導(dǎo)入公有的屬性,方法或類【無(wú)法導(dǎo)入以單下劃線開(kāi)頭(protected)或以雙下劃線開(kāi)頭(private)的屬性,方法或類】  
func() 
#_func() #NameError: name '_func' is not defined 
#__func() #NameError: name '__func' is not defined 

運(yùn)行結(jié)果:

func() is called!

__all__=('func','__func','_A') #放入__all__中所有屬性均可導(dǎo)入,即使是以下劃線開(kāi)頭 
class _A(): 
  def __init__(self,name): 
    self.name=name 
def func():  
  print 'func() is called!'  
def func1():  
  print 'func1() is called!'  
def _func():  
  print '_func() is called!'  
def __func():  
  print '__func() is called!'  
from kk import *   
func()  
#func1() #func1不在__all__中,無(wú)法導(dǎo)入 NameError: name 'func1' is not defined 
#_func() #_func不在__all__中,無(wú)法導(dǎo)入 NameError: name '_func' is not defined 
__func() #__func在__all__中,可以導(dǎo)入 
a=_A('python') #_A在__all__中,可以導(dǎo)入 
print a.name 

運(yùn)行結(jié)果:

func() is called!
__func() is called!
python

#kk.py 
def func(): 
  print 'func() is called!' 
def _func(): 
  print '_func() is called!' 
def __func(): 
  print '__func() is called!' 
#test_kk.py 
from kk import func,_func,__func #可以通過(guò)這種方式導(dǎo)入public,protected,private 
func() 
_func() #NameError: name '_func' is not defined 
__func() #NameError: name '__func' is not defined 

運(yùn)行結(jié)果:

func() is called!
_func() is called!
__func() is called!

#kk.py 
def func(): 
  print 'func() is called!' 
def _func(): 
  print '_func() is called!' 
def __func(): 
  print '__func() is called!' 
#test_kk.py 
import kk #也可以通過(guò)這種方式導(dǎo)入public,protected,private 
kk.func() 
kk._func() #NameError: name '_func' is not defined 
kk.__func() #NameError: name '__func' is not defined 

運(yùn)行結(jié)果:

func() is called!
_func() is called!
__func() is called!

#kk.py 
import sys 
__all__ = ["func"] # 排除了 'sys' 
def func(): 
  print 'func() is called!' 
#test_kk.py 
from kk import * 
#print sys.path #NameError: name 'sys' is not defined 
func() 

運(yùn)行結(jié)果:

func() is called!

如果一個(gè)模塊需要暴露的接口改動(dòng)頻繁,__all__ 可以這樣定義:

__all__ = [
  "foo",
  "bar",
  "egg",
]

最后多出來(lái)的逗號(hào)在 Python 中是允許的,也是符合 PEP8 風(fēng)格的。

模塊中不使用__all__屬性,則導(dǎo)入模塊內(nèi)的所有公有屬性,方法和類 。

模塊中使用__all__屬性,則表示只導(dǎo)入__all__中指定的屬性,因此,使用__all__可以隱藏不想被import的默認(rèn)值。

__all__變量是一個(gè)由string元素組成的list變量。

它定義了當(dāng)我們使用 from <module> import * 導(dǎo)入某個(gè)模塊的時(shí)候能導(dǎo)出的符號(hào)(這里代表變量,函數(shù),類等)。

from <module> import * 默認(rèn)的行為是從給定的命名空間導(dǎo)出所有的符號(hào)(當(dāng)然下劃線開(kāi)頭的變量,方法和類除外)。

需要注意的是 __all__ 只影響到了 from <module> import * 這種導(dǎo)入方式,

對(duì)于 from <module> import <member> 導(dǎo)入方式并沒(méi)有影響,仍然可以從外部導(dǎo)入。

總結(jié)

以上就是本文關(guān)于python中模塊的__all__屬性詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Python內(nèi)建函數(shù)之raw_input()與input()代碼解析、Python面向?qū)ο缶幊袒A(chǔ)解析(一)、python中requests爬去網(wǎng)頁(yè)內(nèi)容出現(xiàn)亂碼問(wèn)題解決方法介紹等,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

  • Python socket套接字實(shí)現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能案例

    Python socket套接字實(shí)現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能案例

    這篇文章主要介紹了Python socket套接字實(shí)現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能,涉及Python socket套接字編寫(xiě)服務(wù)器/客戶機(jī)模式數(shù)據(jù)傳輸相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • 使用PyQt4 設(shè)置TextEdit背景的方法

    使用PyQt4 設(shè)置TextEdit背景的方法

    今天小編就為大家分享一篇使用PyQt4 設(shè)置TextEdit背景的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • matplotlib之pyplot模塊實(shí)現(xiàn)添加子圖subplot的使用

    matplotlib之pyplot模塊實(shí)現(xiàn)添加子圖subplot的使用

    這篇文章主要介紹了matplotlib之pyplot模塊實(shí)現(xiàn)添加子圖subplot的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • python os.rename實(shí)例用法詳解

    python os.rename實(shí)例用法詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于python os.rename實(shí)例用法詳解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2020-12-12
  • 解讀pandas交叉表與透視表pd.crosstab()和pd.pivot_table()函數(shù)

    解讀pandas交叉表與透視表pd.crosstab()和pd.pivot_table()函數(shù)

    這篇文章主要介紹了pandas交叉表與透視表pd.crosstab()和pd.pivot_table()函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python面向?qū)ο缶幊袒A(chǔ)解析(一)

    Python面向?qū)ο缶幊袒A(chǔ)解析(一)

    這篇文章主要介紹了Python面向?qū)ο缶幊袒A(chǔ)解析的相關(guān)內(nèi)容,如果您想對(duì)Python編程的基礎(chǔ)部分有所了解,這篇文章是值得一看的,需要的朋友可以參考下。
    2017-10-10
  • python 每天如何定時(shí)啟動(dòng)爬蟲(chóng)任務(wù)(實(shí)現(xiàn)方法分享)

    python 每天如何定時(shí)啟動(dòng)爬蟲(chóng)任務(wù)(實(shí)現(xiàn)方法分享)

    python 每天如何定時(shí)啟動(dòng)爬蟲(chóng)任務(wù)?今天小編就為大家分享一篇python 實(shí)現(xiàn)每天定時(shí)啟動(dòng)爬蟲(chóng)任務(wù)的方法。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Python實(shí)現(xiàn)批量識(shí)別銀行卡號(hào)碼以及自動(dòng)寫(xiě)入Excel表格步驟詳解

    Python實(shí)現(xiàn)批量識(shí)別銀行卡號(hào)碼以及自動(dòng)寫(xiě)入Excel表格步驟詳解

    這篇文章主要介紹了使用Python實(shí)現(xiàn)高效摸魚(yú),批量識(shí)別銀行卡號(hào)碼并且自動(dòng)寫(xiě)入Excel表格,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-01-01
  • Python3.5迭代器與生成器用法實(shí)例分析

    Python3.5迭代器與生成器用法實(shí)例分析

    這篇文章主要介紹了Python3.5迭代器與生成器用法,結(jié)合實(shí)例形式分析了Python3.5列表生成式、生成器、迭代器等相關(guān)原理與用法,需要的朋友可以參考下
    2019-04-04
  • python中pandas.DataFrame對(duì)行與列求和及添加新行與列示例

    python中pandas.DataFrame對(duì)行與列求和及添加新行與列示例

    pandas是python環(huán)境下最有名的數(shù)據(jù)統(tǒng)計(jì)包,而DataFrame翻譯為數(shù)據(jù)框,是一種數(shù)據(jù)組織方式,這篇文章主要給大家介紹了python中pandas.DataFrame對(duì)行與列求和及添加新行與列的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-03-03

最新評(píng)論