日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python模块中的__all__属性

發(fā)布時間:2024/4/15 python 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python模块中的__all__属性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)自:http://blog.csdn.net/sxingming/article/details/52903377

python模塊中的__all__屬性,可用于模塊導(dǎo)入時限制,如:
from module import *
此時被導(dǎo)入模塊若定義了__all__屬性,則只有__all__內(nèi)指定的屬性、方法、類可被導(dǎo)入。

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

?

[python]?view plaincopy
  • #?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!'??
  • [python]?view plaincopy
  • #test_kk.py??
  • from?kk?import?*??#由于kk.py中沒有定義__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()??
  • 運行結(jié)果:
    python 24
    python 123456
    func() is called!

    func1() is called!

    ?

    [python]?view plaincopy
  • #kk.py??
  • __all__=('A','func')?#在別的模塊中,導(dǎo)入該模塊時,只能導(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!'??
  • [python]?view plaincopy
  • #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??
  • ?

    運行結(jié)果:

    python 24
    func() is called!

    [python]?view plaincopy
  • #kk.py??
  • def?func():?#模塊中的public方法??
  • ????print?'func()?is?called!'??
  • ??????
  • def?_func():?#模塊中的protected方法??
  • ????print?'_func()?is?called!'??
  • ??????
  • def?__func():#模塊中的private方法??
  • ????print?'__func()?is?called!'??
  • ?

    [python]?view plaincopy
  • #test_kk.py??
  • from?kk?import?*??#這種方式只能導(dǎo)入公有的屬性,方法或類【無法導(dǎo)入以單下劃線開頭(protected)或以雙下劃線開頭(private)的屬性,方法或類】????
  • func()??
  • #_func()?#NameError:?name?'_func'?is?not?defined??
  • #__func()?#NameError:?name?'__func'?is?not?defined??
  • 運行結(jié)果:
    func() is called!

    ?

    [python]?view plaincopy
  • __all__=('func','__func','_A')?#放入__all__中所有屬性均可導(dǎo)入,即使是以下劃線開頭??
  • ??
  • 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()????
  • #func1()?#func1不在__all__中,無法導(dǎo)入?NameError:?name?'func1'?is?not?defined??
  • #_func()?#_func不在__all__中,無法導(dǎo)入??NameError:?name?'_func'?is?not?defined??
  • __func()?#__func在__all__中,可以導(dǎo)入??
  • a=_A('python')?#_A在__all__中,可以導(dǎo)入??
  • print?a.name??
  • ?

    運行結(jié)果:

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

    ?

    [python]?view plaincopy
  • #kk.py??
  • def?func():??
  • ????print?'func()?is?called!'??
  • ??????
  • def?_func():??
  • ????print?'_func()?is?called!'??
  • ??????
  • def?__func():??
  • ????print?'__func()?is?called!'??
  • [python]?view plaincopy
  • #test_kk.py??
  • from?kk?import?func,_func,__func??#可以通過這種方式導(dǎo)入public,protected,private??
  • func()??
  • _func()?#NameError:?name?'_func'?is?not?defined??
  • __func()?#NameError:?name?'__func'?is?not?defined??
  • 運行結(jié)果:
    func() is called!
    _func() is called!
    __func() is called!

    ?

    ?

    [python]?view plaincopy
  • #kk.py??
  • def?func():??
  • ????print?'func()?is?called!'??
  • ??????
  • def?_func():??
  • ????print?'_func()?is?called!'??
  • ??????
  • def?__func():??
  • ????print?'__func()?is?called!'??
  • [python]?view plaincopy
  • #test_kk.py??
  • import?kk??#也可以通過這種方式導(dǎo)入public,protected,private??
  • kk.func()??
  • kk._func()?#NameError:?name?'_func'?is?not?defined??
  • kk.__func()?#NameError:?name?'__func'?is?not?defined??
  • 運行結(jié)果:
    func() is called!
    _func() is called!

    __func() is called!

    ?

    [python]?view plaincopy
  • #kk.py??
  • import?sys??
  • ??
  • __all__?=?["func"]??#?排除了?'sys'??
  • ??
  • def?func():??
  • ????print?'func()?is?called!'??
  • [python]?view plaincopy
  • #test_kk.py??
  • from?kk?import?*??
  • ??
  • #print?sys.path?#NameError:?name?'sys'?is?not?defined??
  • func()??
  • 運行結(jié)果:
    func() is called!
    如果一個模塊需要暴露的接口改動頻繁,__all__ 可以這樣定義:
    __all__ = [
    ? ? "foo",
    ? ? "bar",
    ? ? "egg",
    ]
    最后多出來的逗號在 Python 中是允許的,也是符合 PEP8 風(fēng)格的。

    ?

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

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

    __all__變量是一個由string元素組成的list變量。
    它定義了當我們使用 from <module> import * 導(dǎo)入某個模塊的時候能導(dǎo)出的符號(這里代表變量,函數(shù),類等)。
    from <module> import * 默認的行為是從給定的命名空間導(dǎo)出所有的符號(當然下劃線開頭的變量,方法和類除外)。
    需要注意的是 __all__ 只影響到了 from <module> import * 這種導(dǎo)入方式,
    對于 from <module> import <member> 導(dǎo)入方式并沒有影響,仍然可以從外部導(dǎo)入。

    ?


    (完)

    轉(zhuǎn)載于:https://www.cnblogs.com/eternal1025/p/8531975.html

    總結(jié)

    以上是生活随笔為你收集整理的python模块中的__all__属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。