After deleting a builtin function like this, I want to restore it without restarting the interpreter.
>>> import builtins
>>> del builtins.eval
>>> builtins.eval = None
I tried reloading the builtin module using importlib
, that didn't restore eval.
>>> import importlib
>>> importlib.reload(builtins)
<module 'builtins' (built-in)>
>>> eval("5 + 5")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
I also tried to assign a __builtins__
variable from another module. That didn't work as well.
>>> import os
>>> __builtins__ = os.__builtins__
>>> eval()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
Is there a way to restore a builtin function after deleting it?
I think the usage pattern of builtins
is different from what you suggest.
What you typically do is that you re-bind a built-in name for your purpose and then use builtins
to restore the functionality:
eval = None
eval('5 + 5')
# TypeError: 'NoneType' object is not callable
import builtins
eval = builtins.eval
eval('5 + 5')
# 10
or (as commented by @ShadowRanger), even more simply in this specific case:
eval = None
eval('5 + 5')
# TypeError: 'NoneType' object is not callable
del eval
eval('5 + 5')
# 10