Python full stack-day14-modules and packages

Source: Internet
Author: User

First, the module1. Module

1) definition

A collection of functions, in Python the py file is a module

2) Categories of modules

A. Using a py file written in Python

B. C or C + + extensions that have been compiled into shared libraries or DLLs

C. Folders that organize a series of modules (note: There is a __init__.py file under the folder, which is called a package)

D. Built-in modules written with C and linked to the Python interpreter

3) Why practical modules

A. Reusing features

B. Take doctrine, improve development efficiency

2. How to import modules

1) Method One:

Import Module Name

Invoke module function: module name. name in Module name namespace

Once imported, the results of the first import are directly referenced, and the functionality in the file module is not repeated

Three things to do when importing:

A. Creating a file name space

B. Execute the module corresponding file, put the name in the created namespace

C. Get a module name in the current execution file, which points to the namespace created by 1

# importing and invoking functions within a module Import  = time.sleep (5)#  Import module alias  import socketserver as Sockserver # importing multiple modules in one row # import Xx,ss,yy ....

2) Method Two:

From module name import xxx (first name)

First import module, the first two things and import imported method, the third thing directly take xxx (belongs to the name space of the execution file), XXX is the corresponding value in the module

Pros: No prefix, less code

Disadvantage: Easy to duplicate names in the current execution file name space, resulting in the inability to reference

# Import Method  from Import Sleep # Import All  from Import Sleep *

3) two uses of __name__ to differentiate the py file

A.If __name__ = = ' __main__ ' file is executed as a script

B. When the module name is equal, the file is imported as a module

4) Search Path of the module

Memory > built-in Modules > Sys.path

Emphasize:

The first value of Sys.path is the folder where the file is currently executing

The search order shows that the module could not be referenced by the From import module or by a developer who defines the name of the current executable file or the object in the file as the name of the referenced module.

second, the package1. Package

1) definition

A package is a way to organize the Python module namespace by using the '. Module name ', specifically a folder containing __init__.py files, created to be organized with folders/modules

Attention:

A. In python3.x, the import package does not fail even if there is no __init__.py file under the package, and in python2.x, the file must exist under the package

B. The purpose of creating a package is not to run, but to be imported, and the package is just a form of module, which is essentially a module

2. Why use a package

Package is a folder organized by the module, the function of the folder is to organize the modules, as the more we write more, we will not be able to put all the functions in a folder, so through the folder to organize the modules to improve the structure and maintainability of the program

Reference:  Http://www.cnblogs.com/linhaifeng/articles/6379069.html#_label10

Python full stack-day14-modules and packages

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.