2011-04-08 24 views
3

假如我這樣做如何恢復已刪除的庫函數?

import cmath 
del cmath 
cmath.sqrt(-1) 

我得到這個

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'cmath' is not defined 

但是,當我再次導入cmath,我能夠再次使用sqrt

import cmath 
cmath.sqrt(-1) 
1j 

但是當我做了以下

import cmath 
del cmath.sqrt 
cmath.sqrt(-1) 

我得到這個

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'module' object has no attribute 'sqrt' 

即使當我輸入cmath,我再次得到了同樣的錯誤。

是否有可能得到cmath.sqrt

謝謝!

+1

爲什麼要那樣做? – detly 2011-04-08 09:00:27

+1

@detly - 沒有意圖。我只是在學習Python,而我正在使用交互式編譯器。 – bdhar 2011-04-08 10:51:26

回答

4

你會需要reload

reload(cmath) 

...會重新從模塊定義。

import cmath 
del cmath.sqrt 
reload(cmath) 
cmath.sqrt(-1) 

...將正確打印..

1j