Python Learning (6) module-package and python Learning Module

Source: Internet
Author: User

Python Learning (6) module-package and python Learning Module
Python package Definition

To organize modules, multiple modules are divided into one package. The Python processing package is also quite convenient. To put it simply,The package is a folder, but the _ init _. py file must exist in this folder..

The common package structure is as follows:

In the simplest case, you only need an empty _ init _. py file. Of course, it can also execute the package initialization code, or define the _ all _ variable described later. Of course, the package can also contain packages under the package, which is the same as the folder, it is better to understand.

 

Import package

The package import still uses the import, from... import statements, and the structured module namespace using the "dot Module name. The following is an example of a package to understand the specific operation. (Example in the official document)

Suppose you want to design a module set (a "package") to process audio files and sound data in a unified manner. There are several different sound formats (usually identified by their extensions, for example :. wav ,. aiff ,. au) Therefore, to convert different types of file formats, you need to maintain a growing set of packages. You may also want to perform many different operations on the sound data (such as mixing, adding echo, applying the balance function, and creating a person-created effect) therefore, you need to add an infinite stream module to perform these operations. Your package may look like this (grouping through a hierarchical file system ):

      

You can import only the specific modules in the package at a time, for example, import sound. efforts. echo. This imports the sound. effects. echo sub-module. It must be referenced by a complete name.

Sound. effects. echo. echofilter (input, output, delay = 0.7, atten = 4)

You can select a method when importing a package: from sound. effects import echo loads the echo submodule so that it can be used without the package prefix. Therefore, it can be called as follows:

Echo. echofilter (input, output, delay = 0.7, atten = 4)

 

Another variant is used to directly import a function or variable: from sound. effects. echo import echofilter, which loads the echo module again, but can directly call its echofilter () function.

Echo. echofilter (input, output, delay = 0.7, atten = 4)

 

Note that when importing a package from package import item, this item can be a sub-package or another name, such as a function, class, or variable. If not, an ImportError exception is thrown.

When using a syntax such as import item. subitem. subsubitem, these subitems must be packages, and the final subitem can be a package or module, but not a class, function, variable, etc.

 

Import package from *

In theory, we want the file system to find all the sub-modules in the package, and then import them. This may take a long time and result in a boundary effect. The Python solution provides a clear package index.

This index consists of _ init __. py defines the _ all _ variable, which is a list. In the above example, the _ init __. in py, you can define _ all _ = ["echo", "surround", "reverse"]

This means that from sound. effects import * will import the above three sub-modules from the corresponding package. Although the import * method is provided, it is not recommended to use this method in production code.

Package reference

If it is a reference in the sub-package, you can introduce the sub-module in a relative position. The echo module can be referenced as follows:

1 from. import reverse # import reverse2 from .. import frormats # import frormats3 from .. filters import equalizer # import equalizer under the filters module of the parent directory

Search multiple directory packages

The package supports a more special feature. Before executing the code in the package's _ init _. py file, the variable initializes a directory name list. Search for sub-packages and modules.
This function can be used to expand the module set in the package, but is not commonly used.

    

 

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.