Python3.2 official document tutorial-Package

Source: Internet
Author: User

4.4 packages

A package is a way to create a module namespace by using the "vertex Module name. For example, module A. B indicates that A sub-Package B is designed under the package. Just as the application of the module saves the authors of different modules, the global variable names of these different modules need to be considered. However, the module name allows the authors of multiple module packages to not worry about each other's module name (conflict), just like Numpy or python Image Library.

Suppose you want to operate audio files and audio data in a unified manner, you can define a set of multiple modules (packages. The format of a file with different audio frequencies (for example, .wav,. aiff,. au) can be used to convert different file formats by creating and maintaining many modules. You may also have different operations for different file formats (such as mixing, Echo adding, application balancer, and man-created stereo sound ). Therefore, to perform these operations, you need to write another module that will never work. Here is a possible structure of your package.

Sound/Top-level package Top-level file

_ Init _. py Initialize the sound package initializes the audio package

Conversion of formats/Subpackage for file format conversions

_ Init _. py

Wavread. py

Wavwrite. py

Aiffread. py

Aiffwrite. py

6.4. Packages 43

Python Tutorial, Release 3.2.3

Auread. py

Auwrite. py

...

Effects/Subpackage for sound effects sub-Package

_ Init _. py

Echo. py

Surround. py

Reverse. py

...

Sub-packages of the filters/Subpackage for filters Filter

_ Init _. py

Equalizer. py

Vocoder. py

Karaoke. py

...

After the package is imported, python searches for the sub-path of the package through the sys. path directory.

The _ init _. py file is required to make python think that the directory contains a package. This is to prevent a directory containing bad names from inadvertently hiding the limited modules that will appear in the module search path later, such as string. In the simplest case, only one _ init _. py file. Of course, it executes the initialization code for the package or sets the _ all _ variable.

You can only import specific modules from the package at a time. For example:

Import sound. effects. echo

This method loads the sub-module sound. effects. echo. Must be referenced by the full name.

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

Another way to import sub-modules is:

From sound. effects import echo

This method can also load the module echo and can be used without the package prefix. Therefore, it can be applied as follows:

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

Another method of directly importing expected data and variable data is as follows:

From sound. effects. echo import echofilter

At the same time, this method loads the submodule echo, but this method can directly call the function echofilter ();

Echofilter (input, output, delay = 0.7, atten = 4)

Note that when using the from package impport item method, the item is either a sub-module or other names defined in the package, such as functions, classes, or variables. The Import Statement first checks whether it is defined in the package. If not, it assumes a module and then loads it. If not found, an ImportError exception is reported.

On the contrary, when the expression import item. subitem. subsubsubitem is used, all except the last subitem are packages. The last one can make a module or package, but it cannot be a class, function, or variable defined in the module.

6.4.1 import from package *

What happens when a user writes from sound. effects import? In theory, he expected to treat the file system in a friendly way, find all the sub-modules under the package, and import them. It may take more time to import sub-modules. This negative impact may only occur when the sub-modules are explicitly imported.

The only solution for the package author is to provide an accurate index for the package. The Import Statement uses the following conversions. If the _ init _. py code of a package defines a list named _ all _, it is treated as a list of modules to be imported when form package import * is used. When a new version of the release package is released, the package author is responsible for the latest version of the List. If the package author does not find the import * usage in the package. Then they can decide not to do this. For example:

The Sound/effects/_ init _. py file has the following code:

_ All _ = ["echo", "surround", "reverse"]

This means that from sound. effects. Import * will be imported into three sub-modules named display under the sound package.

If _ all _ is not defined, the form sound. effects import * statement does not import all sub-packages from the package sound. effects to the current namespace. It only ensures that the sound. effects package has been imported. Then, import any name defined in the package.

This will introduce all names defined in the _ init _. py file (and explicitly loaded modules ). It will also introduce all the modules in the package loaded by using the previous Import Statement, and think about the following code:

Import sound. effects. echo

Import sound. effects. surround

From sound. effects import *

In this example, the echo and surround modules are also imported to the current namespace, because they are also defined in the sound. effects package when the from... import is executed. (This can also be implemented when _ all is defined)

Note: The habit of importing * from a package or module is not recommended because it often makes the code hard to read. However, it is okay to use it in interactive sessions to reduce input, and some modules are designed to be imported only through feature methods.

Remember: from package import specific_submodule will never be wrong. In fact, this is recommended unless the same name is used for the modules imported in different packages.

4.4.2 package reference

When the package is designed into several sub-packages (like the sound package in the example), you can use absolute import to specify the sub-module or corresponding package. For example, if the module sound. filters. vocoder needs to be used in the echo module of the sound. effects package, it can be used from sound. effects import echo.

You can also write the import Statement in the form of from module import name to write the relevant import. These imports use... to describe the current path and parent package involved in the related import. For example, import from the surrond module:

From. import echo

From .. import formats

From .. filters import equalizer

Note: The relative import is based on the current Module name, because the main module name is always _ main _, so the module in the python program that intends to use the main module must use absolute import.

4.4.3 cross-directory packages

The package supports a special familiarity, _ path _. This will initialize to a name list under the _ init _ path in the package before the file is executed. This variable can be modified to affect the search of sub-packages and modules contained in the package.

Although this feature is not commonly used, it can be used to expand the module set in a package.

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.