2011-04-08 141 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