Python learning notes (3) import and reload Python modules and modules

Source: Internet
Author: User

Python learning notes (3) import and reload Python modules and modules

A module is a core concept of the Python program architecture. A large program is usually presented in the form of multiple module files. A module is designed as a master file or a top-level file to start the entire Python program. Each Python source code file suffixed with. py is a module. Other files can read the content of this module through "import. In general, a module is the encapsulation of variable names. For example, write a module test. py, which contains two variable names: name and age.

name='Aidan'age=27

Then, run the following command in the Python command line:

import testprint(test.name, test.age)

Expected result: Aidan 27

The dir (modelname) function can be used to obtain the available variable names in the module. It contains some Python built-in variable names such as '_ doc _ and _ file __'.

A Python program is usually composed of multiple modules and connected through import. Each module file is a namespace and the variable names of other module files cannot be seen unless the file is imported through import or a variable is imported through from model import varible. This avoids variable naming conflicts, because each module is an independent namespace, similar to a function and its local variables in C language.

In essence, "import" is to load the content of another file in one file, so that another file can be used in the external world. The command is import name. py. The import only works for the first execution of each session. Subsequent import of the same file is invalid even if the file is changed, this is because the file is compiled into bytecode during the first import. The import module must know the detailed path of the module (file search can be performed through sys. the PYTHONPATH variable in path specifies all directories to be searched. Therefore, in order to be simple, put all the files to be imported in the same directory.

If you want to run the same file multiple times in the same session (or the file has changed and must be reloaded), you need to call the "overload" function-reload (name ), before calling the reload function, ensure that the module has been imported successfully through import. See the differences between "function" reload () and "statement" import. reload () is a function and the parameter is the file name of the imported file module. import is a statement without parentheses. The Python overload function allows users to edit and improve code modules during interaction. Therefore, to ensure that the latest code is run, reload () is used first ().

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.