bdhar
bdhar

Reputation: 22983

How to bring back library functions which have been deleted?

Suppose I do this

import cmath
del cmath
cmath.sqrt(-1)

I get this

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

But when I import cmath again, I am able to use sqrt again

import cmath
cmath.sqrt(-1)
1j

But when I do the following

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

I get this

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

Even when I import cmath again, I get the same error.

Is it possible to get cmath.sqrt back?

Thanks!

Upvotes: 3

Views: 434

Answers (1)

Manuel Salvadores
Manuel Salvadores

Reputation: 16525

You'd need reload

reload(cmath)

... will reload definitions from the module.

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

... will correctly print ..

1j

Upvotes: 4

Related Questions