生活随笔
收集整理的這篇文章主要介紹了
python模块中的__all__属性
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
轉(zhuǎn)自:http://blog.csdn.net/sxingming/article/details/52903377
python模塊中的__all__屬性,可用于模塊導(dǎo)入時(shí)限制,如:
from module import *
此時(shí)被導(dǎo)入模塊若定義了__all__屬性,則只有__all__內(nèi)指定的屬性、方法、類(lèi)可被導(dǎo)入。
若沒(méi)定義,則導(dǎo)入模塊內(nèi)的所有公有屬性,方法和類(lèi) 。
?
[python]?view plaincopy
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!'?? [python]?view plaincopy
from?kk?import?*??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!
?
[python]?view plaincopy
__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?called!'??def?func1():??????print?'func1()?is?called!'?? [python]?view plaincopy
from?kk?import?*??a=A('python','24')??print?a.name,a.age??func()?? ?
運(yùn)行結(jié)果:
python 24
func() is called!
[python]?view plaincopy
def?func():?????print?'func()?is?called!'????????def?_func():?????print?'_func()?is?called!'????????def?__func():????print?'__func()?is?called!'?? ?
[python]?view plaincopy
from?kk?import?*??func()?? 運(yùn)行結(jié)果:
func() is called!
?
[python]?view plaincopy
__all__=('func','__func','_A')???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!'??? ?
?
[python]?view plaincopy
from?kk?import?*??????func()????__func()?a=_A('python')?print?a.name?? ?
運(yùn)行結(jié)果:
func() is called!
__func() is called!
python
?
[python]?view plaincopy
def?func():??????print?'func()?is?called!'????????def?_func():??????print?'_func()?is?called!'????????def?__func():??????print?'__func()?is?called!'?? [python]?view plaincopy
from?kk?import?func,_func,__func??func()??_func()?__func()? 運(yùn)行結(jié)果:
func() is called!
_func() is called!
__func() is called!
?
?
[python]?view plaincopy
def?func():??????print?'func()?is?called!'????????def?_func():??????print?'_func()?is?called!'????????def?__func():??????print?'__func()?is?called!'?? [python]?view plaincopy
import?kk??kk.func()??kk._func()?kk.__func()? 運(yùn)行結(jié)果:
func() is called!
_func() is called!
__func() is called!
?
[python]?view plaincopy
import?sys????__all__?=?["func"]????def?func():??????print?'func()?is?called!'?? [python]?view plaincopy
from?kk?import?*????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)的所有公有屬性,方法和類(lè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ù),類(lèi)等)。
from <module> import * 默認(rèn)的行為是從給定的命名空間導(dǎo)出所有的符號(hào)(當(dāng)然下劃線(xiàn)開(kāi)頭的變量,方法和類(lèi)除外)。
需要注意的是 __all__ 只影響到了 from <module> import * 這種導(dǎo)入方式,
對(duì)于 from <module> import <member> 導(dǎo)入方式并沒(méi)有影響,仍然可以從外部導(dǎo)入。
?
(完)
轉(zhuǎn)載于:https://www.cnblogs.com/eternal1025/p/8531975.html
總結(jié)
以上是生活随笔為你收集整理的python模块中的__all__属性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。