Python Learning _09_ Module

Source: Internet
Author: User

Module

The module is the highest organizational unit in Python, and on a physical level, the module is stored as a file, and the module's file name is the module's name. PY, each module has its own namespace.

Python finds the module file by path search, and the path in the PYTHONPATH environment variable is the search path for the Python module, The Sys.path property allows you to see a list of module search paths, and Python searches for the module in the order of the list, so the path in front of the path list does not continue searching after the module has been searched. Because this value is a list, you can either add the path to the list by using the Sys.path.append method, or the Sys.path.insert method, or exclude some paths from the search path by Sys.path.pop method, and so on.

Package

The physical level of the module is a file, the package is a hierarchical special file structure, the special point is that each directory must have a __init__.py file, if not the file, it is just a normal file directory, and can not be imported as a package. This is because the principle of importing the package is actually the __init__.py under the path of the package, which contains the various modules that can be imported.

Module Import and load

Through import, from? Import statement Imports a module, or through as to the module alias, the module is loaded only at the time of the first import, when the module is loaded, is actually the execution of all the statements in the module, at this time, if the module in addition to the defined classes and methods, when there is code in the top-level scope, the code will be executed, Therefore, you need to avoid executing code directly under the top-level scope in the module authoring.

When the module is imported for the first time, it tends to be slow, because when Python imports a new module, it checks to see if there is a. pyc file from the directory where the module is located, and if it does not, it compiles the module into bytecode, which increases the efficiency of the next import.

Python can also import modules from a ZIP file, which is considered a package, but Python will no longer generate. pyc files into the zip file, so the import efficiency is relatively low.

Name space

Namespaces refer to the mapping of identifiers to objects, and Python has two or three active namespaces during execution: The local namespace, the global namespace, and the built-in namespace. All the names of the built-in namespaces are contained in the __BUILTINS__ module, which also contains the __BUITIN__ module, which contains the built-in functions, exceptions, and other properties, each of which is imported into the __builtins__ module before execution. Each module has its own namespace, and when a module is imported, the global namespace of the execution module is loaded, which is why the code under all top-level scopes executes directly when a module is loaded.

namespace and scope relationships: all local namespace names are within the local scope, and all names outside the local namespace are scoped to the global scope. Local namespaces and scopes change as the function is called, but the global namespace is constant. The namespace determines whether a variable name exists, and the scope determines whether a variable name can be accessed. The Globals (), locals () built-in functions can be used to determine which namespace a name belongs to. When accessing a property, the name is looked up from the local namespace, the global namespace, the built-in namespace, and if it is not found, a nameerror error is returned, since the search always looks for the local namespace, so if there is a variable in the local namespace with the same name in the global namespace, The variable "overwrite" in the global namespace (in fact it will not be found).

Built-in functions

__import__ () function, the import statement is actually called the __import__ () function to complete the work, by overriding the function, you can customize the importing algorithm, you can also use the md=__import__ (' module '), equivalent to import Module as AD.

Globals (), locals () returns a dictionary of the global and local namespaces whose keys are the names in the namespace.

The reload () function can be loaded from a new module, as the name implies, from the new execution of the module.

Python Learning _09_ Module

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.