Python reload process instance code parsing, pythonreload

Source: Internet
Author: User

Python reload process instance code parsing, pythonreload

This article focuses on the reload process of Python.

In Python, reload () is used to re-load the previously loaded modules.

reload() Function Syntax:

reload(module)

In Python, the import is only executed once, and subsequent import is only performed in sys. check whether the corresponding module object exists in modules. If you want to re-import the file immediately after modifying the source file and do not want to re-execute the program as a whole, reload will be used here. In practice, it is very useful to test the code Modification result or dynamically change the running behavior reload for services that cannot be stopped.

The reload execution process is as follows:

1) Insys.modules Find the module object of the corresponding module name;

2) Find the corresponding file for the execution File Attribute of the module name and re-compile the file for execution;

3) Update the objects generated during code execution to the attributes of the original module objects in sequence;

We can see the following points from the above execution process:

1) The module object is variable. During reload execution, only the module object is modified. Therefore, after reload, the original reference still points to the module object;

2) during the reload process, the code in the file is re-executed, a new object is created and bound to the module attributes;

3) the import and reload processes in the reload module are irrelevant to the reload process of the module. reload of a module does not cause the import to be reloaded recursively;

4) from * import * In this case, a local variable with the same name points to the object referenced by the attribute of the same name in the module. reload creates a new object (except for some immutable objects) and points to the old variable, generally, you need to assign a value again;

Based on this, test the following code:

# ----- The imported code reloadImported. py ------ print ('excute code in imported file') x = [] y = 1 # ----- test code. py -------- sep = ': 'print (' ---- import module ---- ') import reloadImported from reloadImported import x print ('Id (reloadImported)', id (reloadImported), sep = sep) print ('Id (reloadImported. x) ', id (reloadImported. x), sep = sep) print ('Id (reloadImported. y) ', id (reloadImported. y), sep = sep) print ('Id (x) ', id (x), sep = sep) print (' ---- reload module ----') from importlib import reload (reloadImported) print ('Id (reloadImported) ', id (reloadImported), sep = sep) print ('Id (reloadImported. x) ', id (reloadImported. x), sep = sep) print ('Id (reloadImported. y) ', id (reloadImported. y), sep = sep) print ('Id (x) ', id (x), sep = sep) from reloadImported import x print ('new x id (x )', id (x), sep = sep)

The execution result is as follows:

---- import module ----  excute code in imported file  id(reloadImported): 30322880  id(reloadImported.x): 29841208  id(reloadImported.y): 1502597584  id(x): 29841208 ---- reload module ----  excute code in imported file  id(reloadImported): 30322880  id(reloadImported.x): 29842088  id(reloadImported.y): 1502597584  id(x): 29841208  new x id(x): 29842088 

Summary

The above is all the content about the code parsing of Python reload process instances. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.