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

附: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)美勝于丑陋
顯式勝于隱式
簡(jiǎn)單勝于復(fù)雜
復(fù)雜勝于難懂
扁平勝于嵌套
稀疏勝于緊密
可讀性應(yīng)當(dāng)被重視
盡管實(shí)用性會(huì)打敗純粹性,特例也不能凌駕于規(guī)則之上
不要忽略任何錯(cuò)誤,除非你確認(rèn)要這么做
面對(duì)不明確的定義,拒絕猜測(cè)的誘惑
找到一種最好唯一的方法去解決問(wèn)題
雖然一開(kāi)始這種方法并不是顯而易見(jiàn),因?yàn)槟悴⒉皇?Python 之父
做好過(guò)不做,但沒(méi)有思考的做還不如不做
如果實(shí)現(xiàn)很難說(shuō)明,那它是個(gè)壞想法
如果實(shí)現(xiàn)容易解釋,那它有可能是個(gè)好想法
命名空間是個(gè)絕妙的想法,請(qǐng)多加利用
思考:
為什么交互模式下import this 就直接會(huì)顯示python之禪?(試推測(cè)一下import this 的源代碼)