Python eight, Python module

Source: Internet
Author: User

I. Overview of Python files

1. Python Module overview

You can split a code-heavy program into multiple, independent, but interactive pieces of code that are self-contained, organized snippets of code that are modules

The module behaves as a code file ending with. py in the physical situation

A file is considered a separate module, and a module can also be viewed as a file

The file name of the module is the name of the module plus the extension (. py)

Each module has its own namespace

Python allows the import of other modules for code reuse, which also enables the organization of separate code files into larger program systems

In Python, a module is also an object

All variables defined on the top level of a module are imported as properties of the module being imported


2. Python Program architecture

A python program typically contains a top-level program file and other module files (0, 1, or more)

Top-level files: Contains the main control flow of the program

Module files: Provides a variety of functional components for top-level files or other modules

When the module is first imported (or overloaded), Python immediately executes the top-level program code for the module's text (code that is not inside the function), and the code inside the body of the function is not executed until the function is called .


3, the implementation of the module environment

Modules are imported, but modules can also import and use other modules, which can be written in Python or other programming languages

Modules can contain variables, functions, and classes to do their work, while functions and classes can include variables and other elements


4. Import Module

Only module names can be used when importing modules, not module filenames with. PY Suffixes

Import statement:

Imports the specified entire module, including generating a namespace named after the module name

import Module1[,module2 ...]

It is recommended that an import statement import only one module

Import Module (original module name) as Module_alias (custom module name)

From-import statement:

commonly used to import only some of the properties of a specified module to the current namespace

From module import name1[,name2 ...]

Import and From-import are assignment statements

Import and from are executable statements, similar to Def, so they can be nested in the if test, appear in Def, and so on

Python executes to these statements before it is parsed, which means that all properties from the module will be used only after import is executed

Both import and from are implicit assignment statements

Import assigns the entire module object to a variable name

From assigns one or more variable names to an object of the same name in the module that imports this module

The module is the name space

The namespace of the module can be obtained through the attribute __dict__ or dir (M).

Module properties can be obtained from the dot (.) operator in the format m.attr

A module is a separate scope (local variables are global variables)


This article is from the "Xiexiaojun" blog, make sure to keep this source http://xiexiaojun.blog.51cto.com/2305291/1863135

Python eight, Python module

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.