You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the function has already been loaded in another module at the beginning, then after we do the monkey patch in the original module, that already loaded function's behavior in another module is still not changed.
Eg. mod1.py has func_a, func_a is imported in mod2.py. We monkey patch func_a in mod1.py, but before we do the modification, func_a is already imported into mod2.py for some reason. In this case, the func_a in mod2.py is not updated.
The text was updated successfully, but these errors were encountered:
Yes @zhengjxu, you're correct when saying that this doesn't wortk.
Unfortunately there is nothing that I can do to change this behaviour as this is simply how Python is designed.
When you first execute mod2.py, Python also executes all the modules to import, such as mod1.py, and compiles the result into a file containing all the byte code necessary to use mod2.py. Then, when you import mod1.py from another module to patch it, it will create yet another compiled file for that other module but won't change the modules already compiled, like the one for mod2.py.
In other words, you have to do all the monkey patching operations before you import any of these modules.
If the function has already been loaded in another module at the beginning, then after we do the monkey patch in the original module, that already loaded function's behavior in another module is still not changed.
Eg. mod1.py has func_a, func_a is imported in mod2.py. We monkey patch func_a in mod1.py, but before we do the modification, func_a is already imported into mod2.py for some reason. In this case, the func_a in mod2.py is not updated.
The text was updated successfully, but these errors were encountered: