自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python 炫技操作:模塊重載的五種方法

開發(fā) 后端
由于有 sys.modules 的存在,當(dāng)你導(dǎo)入一個(gè)已導(dǎo)入的模塊時(shí),實(shí)際上是沒有效果的。

環(huán)境準(zhǔn)備

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

  1. $ tree foo 
  2. foo 
  3. └── bar.py 
  4.  
  5. 0 directories, 1 file 

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

  1. print("successful to be imported") 

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

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

  1. >>> from foo import bar 
  2. successful to be imported 
  3. >>> from foo import bar 
  4. >>> 

重載模塊方法一

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

  1. >>> from foo import bar 
  2. successful to be imported 
  3. >>> from foo import bar 
  4. >>> 
  5. >>> reload(bar) 
  6. successful to be imported 
  7. <module 'foo.bar' from 'foo/bar.pyc'> 

如果你使用的 python3 那方法就多了,詳細(xì)請(qǐng)看下面

重載模塊方法二

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

  1. >>> from foo import bar 
  2. successful to be imported 
  3. >>> from foo import bar 
  4. >>> 
  5. >>> import imp 
  6. >>> imp.reload(bar) 
  7. successful to be imported 
  8. <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'> 

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

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

重載模塊方法三

如果你使用的 Python 3.4+,請(qǐng)使用 importlib.reload 方法

  1. >>> from foo import bar 
  2. successful to be imported 
  3. >>> from foo import bar 
  4. >>> 
  5. >>> import importlib 
  6. >>> importlib.reload(bar) 
  7. successful to be imported 
  8. <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'> 

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

  1. >>> from foo import bar 
  2. successful to be imported 
  3. >>> from foo import bar 
  4. >>> 
  5. >>> bar.__spec__.loader.load_module() 
  6. successful to be imported 
  7. <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'> 

重載模塊方法五

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

  1. >>> import foo.bar 
  2. successful to be imported 
  3. >>> 
  4. >>> import foo.bar 
  5. >>> 
  6. >>> import sys 
  7. >>> sys.modules['foo.bar'] 
  8. <module 'foo.bar' from '/Users/MING/Code/Python/foo/bar.py'> 
  9. >>> del sys.modules['foo.bar'] 
  10. >>> 
  11. >>> import foo.bar 
  12. successful to be imported 

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

這是因?yàn)槿绻闶褂?from foo import bar 這種方式,想使用移除 sys.modules 來重載模塊這種方法是失效的。

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

  1. >>> import foo.bar 
  2. successful to be imported 
  3. >>> 
  4. >>> import foo.bar 
  5. >>> 
  6. >>> import sys 
  7. >>> del sys.modules['foo.bar'] 
  8. >>> from foo import bar 
  9. >>> 

 

責(zé)任編輯:趙寧寧 來源: Python編程時(shí)光
相關(guān)推薦

2021-08-18 11:55:25

Python函數(shù)代碼

2020-05-14 10:36:34

Python數(shù)據(jù)開發(fā)

2020-12-21 11:07:58

Python開發(fā)安裝

2020-04-10 08:59:38

Python合并字典語言

2020-11-26 09:14:47

Python操作 轉(zhuǎn)義

2020-03-30 09:51:37

Python數(shù)據(jù)語言

2022-12-29 08:46:15

IT采購投資

2022-12-07 11:24:51

首席信息官IT

2009-07-03 17:48:24

JSP頁面跳轉(zhuǎn)

2025-04-25 08:55:00

Pod運(yùn)維

2024-07-09 08:43:52

2020-08-06 13:19:10

IBM多云管理

2020-12-03 14:40:10

云管理

2019-12-12 20:03:08

PythonC語言編程語言

2011-04-21 10:08:34

2022-01-10 06:52:59

查詢MySQL字段

2021-11-02 07:54:40

List分片Java

2022-11-23 13:46:02

云支出云計(jì)算

2020-04-02 10:45:48

多云云計(jì)算云平臺(tái)

2015-09-10 09:30:54

Java多線程同步
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)