The import statement in Python can be imported into the module file, but the import statement executes the code in the module file only the first time it is imported, and then the imported module file is stored in memory, and when it is imported again, Python takes the module file directly from memory without executing the contents of the module file, and the Reload function forces Python to re-import and execute the module file.
Suppose there is a module file a.py:
def changer (): Print ("firstVersion")
Then execute the following code in an interactive console:
>>>Import a>>>a.changer ()"firstVersion"
Then we do not exit the interactive console and then modify the a.py:
def changer (): pirnt ("Second Version")
Then execute the following code on to the interactive console:
>>>Importa>>>a.changer ()#the module files that have been imported are taken directly from the memory and will not be executed"First Version">>> fromImpImportReload#for Python 3.X, Reload is not a built-in function, but a function of the Imp module>>>Reload (a)<module'a'Form'/home/chaochao/python/a.py'>>>>a.changer ()#changes to the module file a.py are reflected."Sencond Version"
Precautions
1 because import imports the Moudle file, The use of the use of module.attr, and reload will be forced to run the module file, it will be imported using Import module file has an impact, because reload executes the module file, module.attr value will be overwritten;
2 because of the module file from the import, it is essentially an assignment operation, that is, in the current file (that is, the file that executes the FROM statement) attr = Module.attr, then, the reload function has no effect on the FROM statement before the reload run, in other words, the variables in the current file (that is, the file that executes the FROM statement) are already two different variables attr and module.attr references;
3 assuming a.py own import b.py, then reload (a) does not reload (b), that is, reload does not have transitivity
4 Use reload premise, is the reload module, previously used import or from the import succeeded, otherwise, reload will not take effect
5 for Python 2.X, Reload is a built-in function, while Python 3.X moves the reload function into the Imp module
The reload function in Python