How to Implement modular modification in the Python Programming Language

Source: Internet
Author: User

We need to learn a lot about the Python programming language. We need to master a lot of knowledge, especially in the modification and construction of modular structures. Next, let's take a detailed look at the relevant technical knowledge.

Module search path: When importing a module called spam, the interpreter first searches for the module named spam in the current directory. in the environment variable PYTHONPATH refers to the PATH list in the environment variable PATH. If PYTHONPATH is not set or the file is not found, search for the installation directory. in UNIX, it is usually the.:/usr/local/lib/python programming language. In fact, the interpreter is the path Directory Search module specified by the sys. path variable. This variable contains the input script or the current directory by default during initialization), PATHPATH, and installation directory.

A package usually uses a structured module namespace with the "dot Module name. For example, the module named A. B represents the sub-module named "B" in the package named ". Just as using modules to save different module architectures can avoid conflicts between global variables, using the dot Module name to save different class Library architectures such as NunPy or Python Imaging Library can avoid name conflicts between modules.
When importing a module, the Python programming language searches for the subdirectories of the package through the directory list in sys. path.

There must be a _ init __. the existence of the py file allows the Python programming language to regard the directory as a package; this is to prevent some directories from using generic names such as "string" and inadvertently overwriting the correct module in the subsequent module search path. In the simplest case, __init _. py can be just an empty file, but it may also contain the package initialization code or set the _ all _ variable.

Package users can import valid modules from the package, for example:

 
 
  1. import Sound.Effects.echo  

In this way, the Sound. Effects. echo sub-module is imported. It must be referenced by a complete name.

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

You can select the following method when importing a package:

 
 
  1. from Sound.Effects import echo  

In this way, the echo submodule is loaded and can be used without the package prefix. Therefore, it can be called as follows:

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

Another variant is used to directly import functions or variables:

 
 
  1. from Sound.Effects.echo import echofilter  

In this way, the echo submodule is loaded again, but the echofilter () function can be called directly:

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

Note that when you import a package using the from package import item method, this sub-item can be either a sub-module in the package or a sub-package ), it can also be other names defined in the package, such as functions, classes, or variables. The import Statement first checks whether the package contains this subitem. If not, it assumes that this is a module and tries to load it. If it is not found, an ImportError exception is thrown.

Instead, use import item. subitem. subsubitem syntax, these subitems must be packages, and the last subitem can be a package or module, but not a class, function, or variable defined in the previous subitem.

The Python programming language makes up a file of functions with similar functions, which is called a module.

  • Technical Comparison Between the Python programming language and Java
  • Comparison of Python function and Ruby related technologies
  • Basic Application of Python to generate random numbers and random strings
  • Benefits of Python Image Processing
  • Basic Steps for installing and configuring Python

The import search path is based on the sys. path value. Add a path through append. The import Statement can import the file name to the current file and then call the function of the file. Only the imported file name). from... import sequence remains unchanged, used to initialize the module, only executed when the module is imported for the first time. _ Name __; modify the global variables in the module using the modlename. itemname method.

Byte compiling. The specified script file name in the command line does not generate the. pyc file. sys. ps1 and sys. ps2 make sense under interaction.

Dir () lists the names defined under the module, excluding the built-in ones.) _ builtin __

The Python programming language places files with similar functions in the same folder, which is called a package.

The from package import module can omit the preceding package name. Import is only the name of the imported package.

The _ init _. py file indicates that the folder is 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.