空間網(wǎng)站大全站點
- 導入(無論是通過import或from語句)只會使模塊在流程中第一次導入時,加載和執(zhí)行該模塊的代碼。
- 之后的導入只會使用已加載的模塊對象,而不會重載或重新執(zhí)行文件的代碼。
- reload函數(shù)會強制已加載的模塊的代碼重新載入并重新執(zhí)行。此文件中新的代碼的賦值語句會在適當?shù)牡胤叫薷默F(xiàn)有的模塊對象。
- reload會在模塊當前命名空間內(nèi)執(zhí)行模塊文件的新代碼。重新執(zhí)行模塊文件的代碼會覆蓋其現(xiàn)有的命名空間,并非進行刪除而進行重建。
reload實例
reload應(yīng)用在什么地方?
舉個例子來說,你已經(jīng)導入了一個anan.py文件(交互模式下),你再去修改anan.py就會發(fā)現(xiàn)在當前交互模式下依然是原來的anan.py,所以就需要用到reload.
看下面這個簡單的例子:

附:python之禪
import this
'''
The Zen of python,by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
ALthough that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
'''
優(yōu)美勝于丑陋
顯式勝于隱式
簡單勝于復雜
復雜勝于難懂
扁平勝于嵌套
稀疏勝于緊密
可讀性應(yīng)當被重視
盡管實用性會打敗純粹性,特例也不能凌駕于規(guī)則之上
不要忽略任何錯誤,除非你確認要這么做
面對不明確的定義,拒絕猜測的誘惑
找到一種最好唯一的方法去解決問題
雖然一開始這種方法并不是顯而易見,因為你并不是 Python 之父
做好過不做,但沒有思考的做還不如不做
如果實現(xiàn)很難說明,那它是個壞想法
如果實現(xiàn)容易解釋,那它有可能是個好想法
命名空間是個絕妙的想法,請多加利用
思考:
為什么交互模式下import this 就直接會顯示python之禪?(試推測一下import this 的源代碼)