Introduction to the modular import of Python (i)

Source: Internet
Author: User

Usage:

Import Module 1[, Module 2, Module 3 ...]

OS Top-level modules
Os.path Non-top modules
As equals renaming

The essence of import:

The interpreter is responsible for loading the module separately, initializing it separately, generating a Module object, the current scope generating an identifier of the same name, mapping to the Module object, and then using this identifier to access the resources of the module object. The module is not directly referenced, or the source code of the module is loaded into the current space.

The include in the HTML language means that the source code of the other files is included directly to generate a new source file.

Example 1:
' Functools '] <module ' Functools ' from ' c:\\python\\lib\\functools.py ' >//This Module object is a modules module type <function wraps at 0x0000020a133f1d90>[finished in 0.4s]

  

The class, method, and property of the module can be invoked with "." After import, because the module is initialized to an object.

Example 2:
'os ']

  

Example 3:
' P '] Ntpath[finished in 0.3s]

  

Summarize:

1. When importing a top-level module directly (such as import OS), the interpreter is responsible for loading the module separately, initializing the module, generating a Module object, and binding an identifier with the same name in the current namespace.

When used, you need to use the format "module name. Method ()".

2. When a non-top-level module is imported (such as import Os.path), only the object identifier of the top-level module is generated in the current namespace and the path identifier is not generated.

When used, it can only be used with the full hierarchical relationship of "os.path.exists ()".

3. When the imported non-top-level module uses an as statement (such as import Os.path as P), the current namespace is bound to the object identifier "P".

When used, the module resources can be used in a "p.exists ()" manner.

Introduction to the modular import of Python (i)

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.