The reload function in Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.