I created a Python module with a single function that just prints 'a!'. I opened the Python interpreter and imported the module in 2 different syntaxes
>>> import a >>> from a import func >>> func() a! >>> a.func() a!
At that moment I changed the function to print something else and then appreciated again
>>> func() a! >>> a.func() a!
This, of course, is expected since the module was not rebooted. Then I reloaded the module and expected both functions to be updated:
>>> reload(a) <module 'a' from 'a.py'> >>> a.func() aasd! >>> func() a!
Only a.func seems to be updating. I always thought that Python only stores one instance of the same module, but now it seems to be two. I conducted additional testing to verify my claim, and added a print instruction at the top level of the module, then restarted the interpreter again and imported again:
>>> import a module imported >>> import a >>> from a import func
This bothers me even more, as I was expecting to βimport the moduleβ twice. The third experiment I did was a global variable experiment:
>>> import a module imported >>> from a import GLOBAL_VAR >>> GLOBAL_VAR = 5 >>> a.GLOBAL_VAR 1 >>> GLOBAL_VAR 5 >>> GLOBAL_VAR is a.GLOBAL_VAR False
So, there is one instance of the module, but different instances of the objects inside? How can I implement Gevent monkey patching with this behavior?
source share