python中模組的 all

2021-07-23 23:09:08 字數 4789 閱讀 2736

python模組中的__all__屬性,可用於

模組匯入時限制,如:

from module import *

此時被匯入模組若定義了__all__屬性,則只有__all__內指定的屬性、方法、類可被匯入。

若沒定義,則匯入模組內的所有公有屬性,方法和類 。

# 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中沒有定義__all__屬性,所以匯入了kk.py中所有的公有屬性、方法、類

a=a('python','24')

print a.name,a.age

b=b('python',123456)

print b.name,b.id

func()

func1()

執行結果:

python 24

python 123456

func() is called!

func1() is called!

#kk.py

__all__=('a','func') #在別的模組中,匯入該模組時,只能匯入__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__屬性,只能匯入__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

執行結果:

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 * #這種方式只能匯入公有的屬性,方法或類【無法匯入以單下劃線開頭(protected)或以雙下劃線開頭(private)的屬性,方法或類】

func()

#_func() #nameerror: name '_func' is not defined

#__func() #nameerror: name '__func' is not defined

執行結果:

func() is called!

__all__=('func','__func','_a') #放入__all__中所有屬性均可匯入,即使是以下劃線開頭

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__中,無法匯入 nameerror: name 'func1' is not defined

#_func() #_func不在__all__中,無法匯入 nameerror: name '_func' is not defined

__func() #__func在__all__中,可以匯入

a=_a('python') #_a在__all__中,可以匯入

print a.name

執行結果:

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 #可以通過這種方式匯入public,protected,private

func()

_func() #nameerror: name '_func' is not defined

__func() #nameerror: name '__func' is not defined

執行結果:

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 #也可以通過這種方式匯入public,protected,private

kk.func()

kk._func() #nameerror: name '_func' is not defined

kk.__func() #nameerror: name '__func' is not defined

執行結果:

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()

執行結果:

func() is called!

如果乙個模組需要暴露的介面改動頻繁,__all__ 可以這樣定義:

__all__ = [

"foo",

"bar",

"egg",

]最後多出來的逗號在 python 中是允許的,也是符合 pep8 風格的。

模組中不使用__all__屬性,則匯入模組內的所有公有屬性,方法和類 。

模組中使用__all__屬性,則表示只匯入__all__中指定的屬性,因此,使用__all__可以隱藏不想被import的預設值。

__all__變數是乙個由string元素組成的list變數。

它定義了當我們使用 from import * 匯入某個模組的時候能匯出的符號(這裡代表變數,函式,類等)。

from import * 預設的行為是從給定的命名空間匯出所有的符號(當然下劃線開頭的變數,方法和類除外)。

需要注意的是 __all__ 只影響到了 from import * 這種匯入方式,

對於 from import 匯入方式並沒有影響,仍然可以從外部匯入。

(完)

python中模組的 all

python模組中的 all 屬性,可用於模組匯入時限制,如 from module import 此時被匯入模組若定義了 all 屬性,則只有 all 內指定的屬性 方法 類可被匯入。若沒定義,則匯入模組內的所有公有屬性,方法和類 kk.py all a func 在別的模組中,匯入該模組時,只能...

python中模組的 all

python模組中的 all 用於模組匯入時限制,如 from module import 此時被匯入模組若定義了 all 屬性,則只有 all 內指定的屬性 方法 類可被匯入 若沒定義,則匯入模組內的所有公有屬性,方法和類。1.例項1 bb.py class a def init self,nam...

Python模組匯入時全域性變數 all

這位大佬已經寫得很好了 python中乙個py檔案就是乙個模組,all 變數是乙個特殊的變數,可以在py檔案中,也可以在包的 init py 現。1 在普通模組中使用時,表示乙個模組中允許哪些屬性可以被匯入到別的模組中,如 全域性變數 函式和類等。如下,test1.py和main.py 檔案 tes...