婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > python 模塊重載的五種方法

python 模塊重載的五種方法

熱門標簽:地圖標注微信發(fā)送位置不顯示 地圖制圖標注位置改變是移位嗎 浙江電銷卡外呼系統(tǒng)好用嗎 房產(chǎn)電銷外呼系統(tǒng) 上海機器人外呼系統(tǒng)哪家好 地圖標注的意義點 南京銷售外呼系統(tǒng)軟件 蓋州市地圖標注 315電話機器人廣告

環(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 被導(dǎo)入一次,就被執(zhí)行一次 print

禁止重復(fù)導(dǎo)入

由于有 sys.modules 的存在,當你導(dǎo)入一個已導(dǎo)入的模塊時,實際上是沒有效果的。

>>> 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ù)導(dǎo)入的是 sys.modules,那我們只要將已導(dǎo)入的包從其中移除是不是就好了呢?

>>> 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 模塊重載的五種方法的詳細內(nèi)容,更多關(guān)于python 模塊重載的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • Python正確重載運算符的方法示例詳解
  • Python運算符重載詳解及實例代碼
  • Python運算符重載用法實例分析
  • Python運算符重載用法實例
  • python 運算符 供重載參考
  • python中reload重載實例用法
  • python3代碼中實現(xiàn)加法重載的實例
  • Python importlib模塊重載使用方法詳解
  • python中有函數(shù)重載嗎
  • 在Python中實現(xiàn)函數(shù)重載的示例代碼
  • Python的運算符重載詳解

標簽:雙鴨山 陽泉 日照 克拉瑪依 臨汾 赤峰 貴州 金華

巨人網(wǎng)絡(luò)通訊聲明:本文標題《python 模塊重載的五種方法》,本文關(guān)鍵詞  python,模塊,重載,的,五種,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python 模塊重載的五種方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于python 模塊重載的五種方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 阜康市| 华池县| 香河县| 汝阳县| 新津县| 孟津县| 柯坪县| 商丘市| 凤城市| 保亭| 邛崃市| 五华县| 泉州市| 阿拉善盟| 东山县| 金平| 钟山县| 双柏县| 汉川市| 桃园市| 通榆县| 康保县| 建始县| 红安县| 怀远县| 交城县| 昌黎县| 册亨县| 易门县| 成都市| 方城县| 清徐县| 玉门市| 息烽县| 塘沽区| 综艺| 车致| 通化市| 江源县| 绥滨县| 平陆县|