Python中使用語句匯入模組或包的機制研究

2022-09-28 03:24:10 字數 3341 閱讀 7582

這篇文章討論了python的from import *和from import *,它們怎麼執行以及為什麼使用這種語法(也許)是乙個壞主意。

從乙個模組匯入全部

from import * means意味著「我希望能訪問中我有許可權訪問的全部名稱」。例如以下**something.py:

# something.py

public_variable = 42

_private_variable = 141

def public_function():

print("i'm a public function! yay!")

def _private_function():

print("ain't nobody accessing me from another module...usually")

class publicclass(object):

pass

class _weirdclass(object):

pass

在python直譯器中,我們可以執行from something import *,然後看到如下的內容:

>>> from something import *

>>> public_variable

42>>> _private_variable

...nameerror: name '_private_variable' is not defined

>>> public_function()

"i'm a public function! yay!"

>>> _private_function()

...nameerror: name '_private_程式設計客棧function' is not defined

>>> c = publicclass()

&ozorugt;>> c

>>> c = _weirdclass()

...nameerror: name '_weirdclass' is not defined

from something import *從something中匯入了除了以_開頭名稱外的其他所有名稱,按照規範,_開始的名稱是私有的所以未被匯入。

嗯,不是特別糟!還有什麼?

上面沒提到__all__是什麼。__all__是乙個字串列表,指定了當from import *被使用時,模組(或者如後文會提到的包)中的哪些符號會被匯出。如果我們不定義__all__(我們在上面的something.py就沒定義),import *預設的匯入方式是匯入除了下劃線(_)開頭的所有名稱。再說一次,程式設計慣例上下劃線表示乙個符號是私有的,不匯入是合理的。讓我們來看看在something.py中定義我們自己的__all__會發生什麼。

# something.py

__all__ = ['_private_variable', 'publicclass']

# the rest is the same as before

public_variable = 42

_private_variable = 141

def public_function():

print("i'm a function! yay!")

def _private_function():

print("ain't nobody accessing me from an程式設計客棧other module...usually")

class publicclass(object):

pass

class _weirdclass(object):

pass

現在,我們期望from something import *只會匯入_private_variable和publicclass:

>>> from something import *

>>> public_variable

42>>> _private_variable

...nameerror: name '_private_variable' is not defined

>>> public_function()

"i'm a public function! yay!"

>>> _private_function()

...nameerror: name '_private_function' is not defined

>>> c = publicclass()

>>> c

>>> c = _weirdclass()

...nameerror: name '_weirdclass' is not defined

包是怎樣的呢?

當從乙個包中匯入全部時,__all__的做法和模組基本一樣,不過它處理的是包中的模組(而不是把模組中的名都匯入)。所以當我們使用from import *.時__all__說明了所有需要被匯入當前命名空間的模組。

不同之處在於,如果你在乙個包的__init__.py裡面沒有宣告__all__,from import *語句不會匯入任何東西(這個說法也不全對,正確的說法在此)

但是,這有什麼不好?

繼續讀之前,在你的python直譯器中,執行import this,再讀一遍python之禪(在你孩子每晚睡前也要讀給他們)。

明確比含糊要好。

from import * 是不明確的。它沒告訴我們我們正在匯入什麼或者我們把什麼帶入當前命名空間了。更ozoru好的做法是顯式地匯入我們需要的全部名稱。這種方式下,讀者(非常可能是未來的你自己)就不會困惑於你**中使用的乙個變數/方法/類/其他東西是哪兒來的,這也告訴了我們下一點:

可讀性很重要

即使你需要匯入很多東西,乙個乙個顯式地匯入也更清楚。使用pep 328:

from tkinter import (tk, frame, button, entry, canvas, text,

left, disabled, normal, ridge, end)

你現在就能明確知道你的命名空間裡有什麼,使用ctrl+f能很快地告訴你它們是哪兒來的。

同時,你還總是要承擔模組/包作者更改list內容(加/減東西)的風險。也就是下面兩者之一:

作者從__all__裡刪除了乙個字串。如果你的**使用了那個名字,你的**就會報出nameerror的錯誤,並且很難發現為什麼。

作者在__all__裡加入了很多東西。你也許不需要這些增加的內容,所以你只是讓這些你不關心的東西佔滿了你的命名空間。他們甚至在你不注意的時候會替代其他同名內容。

當然,有時候從模組或者包中匯入全部內容是有用的。不過,這麼做之前三思。從我的經驗來看,這麼做通常只是因為懶。

本文標題: python中使用語句匯入模組或包的機制研究

本文位址:

Python 學習 import語句匯入模組

簡單的學習一下呼叫外部的模組檔案。在python中,模組是一種組織形式,它將彼此有關係的pyrhon 組織到乙個個獨立的檔案當中,模組可以包含可執行 函式,和類或者是這些東西的組合。當我們建立乙個python 原始檔的時候,模組的名字就是不帶 py 字尾的檔名。乙個模組建立之後,我們可以從另乙個模組...

Oracle中使用語句新增或刪除字段

alter table 表名 add 新列名 資料型別 完整性約束 drop 完整性約束名 modify 列名 資料型別 for example alter table students add scome date 在students 表中插入型別為 date 的列 scome eg altert...

with語句在Python中使用

引言 with語句生於python2.5,通過 from future import with statement 匯入後使用 2.6以後無需匯入直接使用 with 語句適用於對資源進行訪問的場合,確保不管使用過程中是否發生異常都會執行必要的 清理 操作,釋放資源 用途 最常用的兩個地方,檔案使用後...