Python's Reload () method __python

Source: Internet
Author: User
Tags in python
The reload () function loads the previously imported modules again. Reloading (reload) includes the parsing process and initialization process that was applied when the module was originally imported. This allows the changed Python module to be reloaded without exiting the interpreter.

A number of points to note:
1. If the module is syntactically correct, but fails during initialization, the import process does not correctly bind the module's name to the symbol table. At this point, you must load the module with the import () function before the module can be reloaded.
2. Reloaded modules do not delete entries in the symbol table for the original old version module.
3. If a module utilizes from ... Import ... method to import an object from another module, the reload () function does not redefine the imported object, you can use the import ... form to avoid this problem.

4. The Reload module that provides the class does not affect any saved instances of the provided class---the saved instance will continue to use the original method definition, only the new instance of the class uses the new format. This principle applies equally to derived classes.


In Python, every Python file that ends with. PY is a module. Other files can be imported into a module to read the contents of the module. The import is essentially loading another file and being able to read the contents of that file. The content of a module can be used by the outside world through such attributes.

This modular approach makes the module a core concept of the Python program architecture. Larger programs often appear in the form of multiple module files and import tools for other module files. One of the module files is designed as a master file, or a top-level file (that is, a file that can run the entire program after startup).

By default, after the module is imported for the first time, no other imports are valid. If you change and save the module's source code file in another window at this time, you cannot update the module. The reason for this design is that the import is a costly operation (the import must find the file, compile it into bytecode, and run the code) so that each file, every program cannot be run more than once.

What if you want to make Python run the file again in the same session? This requires invoking the reload function in the IMP standard library module. As shown below

 

1 From IMP import reload reload (MyModule)

  

This allows the MyModule module to be reloaded so that the modifications are valid.

Note: The reload function wants to obtain the name of a module object that has already been loaded, so make sure that you have successfully imported the module before overloading it.

Description: Python 3.0 moves the reload built-in function to the IMP standard library module. It still overloads the file as before, but it must be imported to use it. In Python 3.0, run import imp and use Imp.reload (m), or, as shown above, run from IMP Import and use reload (m)

Machine Learning Combat P22 page

>>>reload (KNN) instead

In Python 3.5.2

>>>import importlib

>>>importlib.reload (KNN)

In Python2, Reload () is a built-in function that can be used directly.

However, in Python3, direct use of reload () will be an error and need to be imported from Imp.

Usage is

[HTML] view plain copy print? >>>from Imp Import reload >>>reload (XYZ)


Related Article

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.