Python模塊重載如何實現(xiàn)

本文小編為大家詳細介紹“Python模塊重載如何實現(xiàn)”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“Python模塊重載如何實現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比沙灣網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式沙灣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋沙灣地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。

環(huán)境準備

新建一個 foo 文件夾,其下包含一個 bar.py 文件

$ tree foo foo └── bar.py  0 directories, 1 file

bar.py 的內(nèi)容非常簡單,只寫了個 print 語句

print("successful to be imported")

只要 bar.py 被導入一次,就被執(zhí)行一次 print

禁止重復(fù)導入由于有 sys.modules 的存在,當你導入一個已導入的模塊時,實際上是沒有效果的。

>>> from foo import bar successful to be imported >>> from foo import bar >>>

重載模塊方法一

如果你使用的 python2(記得前面在 foo 文件夾下加一個 __init__.py),有一個 reload 的方法可以直接使用

>>> from foo import bar successful to be imported >>> from foo import bar >>> >>> reload(bar) successful to be imported <module 'foo.bar' from 'foo/bar.pyc'>

如果你使用的 python3 那方法就多了,詳細請看下面

重載模塊方法二

如果你使用 Python3.0 -> 3.3,那么可以使用 imp.reload 方法

>>> from foo import bar successful to be imported >>> from foo import bar >>> >>> import imp >>> imp.reload(bar) successful to be imported <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'>

但是這個方法在 Python 3.4+,就不推薦使用了

<stdin>:1: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses

重載模塊方法三

如果你使用的 Python 3.4+,請使用 importlib.reload 方法

>>> from foo import bar successful to be imported >>> from foo import bar >>> >>> import importlib >>> importlib.reload(bar) successful to be imported <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'>

重載模塊方法四如果你對包的加載器有所了解,還可以使用下面的方法

>>> from foo import bar successful to be imported >>> from foo import bar >>> >>> bar.__spec__.loader.load_module() successful to be imported <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'>

重載模塊方法五

既然影響我們重復(fù)導入的是 sys.modules,那我們只要將已導入的包從其中移除是不是就好了呢?

>>> import foo.bar successful to be imported >>> >>> import foo.bar >>> >>> import sys >>> sys.modules['foo.bar'] <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'> >>> del sys.modules['foo.bar'] >>> >>> import foo.bar successful to be imported

有沒有發(fā)現(xiàn)在前面的例子里我使用的都是 from foo import bar,在這個例子里,卻使用 import foo.bar,這是為什么呢?

這是因為如果你使用 from foo import bar 這種方式,想使用移除 sys.modules 來重載模塊這種方法是失效的。

這應(yīng)該算是一個小坑,不知道的人,會掉入坑中爬不出來。

>>> import foo.bar successful to be imported >>> >>> import foo.bar >>> >>> import sys >>> del sys.modules['foo.bar'] >>> from foo import bar >>>

讀到這里,這篇“Python模塊重載如何實現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文名稱:Python模塊重載如何實現(xiàn)
分享網(wǎng)址:http://bm7419.com/article44/jjssee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站維護、外貿(mào)建站網(wǎng)站收錄小程序開發(fā)全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)