Reputation: 173
I'm exploring the concept of namespace in Python and I can't explain the following: reloading builtins
does not effectively reload the module. Here's an example:
import importlib as il
import os
import mymodule
mymodule.x = 6
print(mymodule.x)
mymodule = il.reload(mymodule)
print(mymodule.x)
import builtins
builtins.print = lambda x : os.system('echo hello')
builtins.print('hi')
builtins = il.reload(builtins)
builtins.print('hi')
Where mymodule simply contains the assignment x = 5
. The output is:
6
5
hello
hello
Maybe it's a dummy question, but what is the reason for this kind of behavior?
Upvotes: 1
Views: 982
Reputation: 59148
From the docs:
It is generally not very useful to reload built-in or dynamically loaded modules. Reloading
sys
,__main__
,builtins
and other key modules is not recommended. In many cases extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded.
Upvotes: 2