Introduction to the concept of Python modules

Source: Internet
Author: User

Module

Module: The essence is a. py file
Divided into three parts: built-in modules, third-party modules, custom modules

Modules: top-level files

  • python module
    Python module can split a code-heavy program into multiple organized, independent, but interactive pieces of code that are self-contained, organized snippets of code that are modules, The module is physically represented as a code file that ends in. py.
    A file is treated as a standalone module, and a module can be considered a file
    the filename of the module is the name of the module plus the extension. PY
    Each module has its own namespace
    Python allows "Import" other modules for code reuse, which also enables the organization of separate code files into larger program systems
    Python, modules also objects
    All variables defined at the top level of a module are imported as properties of the module being imported

  • Python program architecture
    A python program typically includes a top-level program file and other module files (0, one, 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 file (code that is not inside the function). The code in the body of the function is not executed until the function is called

  • Execution environment of the module
    Modules are imported, but modules can also be imported and used by other modules, which can be written in Python or other programming language modules can contain variables, functions, and classes to limit their work, while functions and classes can include variables and other elements

  • Import module
    You can only use module names when importing modules, instead of using the module file name import statement with the. py suffix to import the entire module specified, including generating a namespace named after the module name import module1[,module2[ ,... Modulen]] It is recommended that an import statement import only one module imported modules as Module_alias #为模块起个别名 from-import statements are commonly used to import only some of the properties of a specified module to the current namespace from Module import name1[,name2[,... Namen]]

  • import and From-import are assignment statements
    Import and from are executable statements, similar to Def, so they can appear in def in the if test, and so on. Python executes these statements before parsing them, which means that all properties from the module are only allowed after the import statement is executed before the import and from are implicitly assigned statements import assigns the entire module object to a Pro variable name From assigning one or more variable names to an object module of the same name in the module that imports this module, the namespace module namespace can be dict or dir (M) Gets the module properties by Dot (.) Operator is obtained, the format of the M.attr module is a separate scope (local variables are global variables)

  • import working mechanism
    Import statement imports the specified module or performs three steps to find the module file under the specified path, the search module file is compiled into a bytecode file when the import is compiled, Therefore, the. PYc bytecode file for the top-level file is discarded after internal use, and only the files that are imported are left behind. The code of the PYc file execution module to create all the statements in the module file of its defined object are executed sequentially, from the beginning to the beginning, and any assignment to the variable name in this step, will produce the properties of the resulting module file Note: The module will only perform the above steps on the first import. The subsequent import operation is simply to extract the loaded module object in memory reload () to reload the module

  • Working mechanism of Import
    Import statement when importing a specified module or performing three steps to find a module file under the specified path the search module files are compiled into a bytecode file import, so the. PYc bytecode file for the top-level file is discarded after internal use, and only the files that are imported are left. pyc file Executing the code of the module to create all the statements in the object module file that it defines is executed sequentially, from beginning to the beginning, and any assignment to the variable name in this step results in the properties of the resulting module file. Note: The module will only perform the previous step on the first import. The subsequent import operation is simply to extract the loaded module object in memory reload () can be used to reload the module

Introduction to the concept of Python modules

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.