Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring back library functions which have been deleted?

Tags:

python

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!

like image 341
bdhar Avatar asked Jan 30 '26 05:01

bdhar


1 Answers

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
like image 111
Manuel Salvadores Avatar answered Jan 31 '26 20:01

Manuel Salvadores



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!