6. Python Module

Source: Internet
Author: User

6-1. Understanding the Python Module

1. What is a module? A function is a program that can implement one or more functions, a module is an extension of function function, a module is a program block that can implement one or more functions, in other words, functions and modules are used to implement function, but the scope of the module is wider than the function, and multiple functions can be reused in the module; 2, how to import the module? Python in a program to use a module, you can not directly use the function inside the module, before using the module, you must first import the specified module; 3, sys module:There are some modules in python that don't need to be defined by ourselves, and Python has its own modules, which are created with the installation of Python, which we call standard library modules, and many modules in the standard library, such as those related to e-mail, OS-related, etc. We refer to these modules in the standard library that relate to system functions as SYS modules; For example:ViewVersion InformationTo view the files that are currently runningDirectory Address; viewversion Information for window operating system; View the importedModule Keywords; 6-2, byte compilation 1. What is the. pyc file for Python? Since the computer only knows the binary machine language, there are two ways to execute the Python module, one is to compile the contents of the module into a binary language, then execute the binary language program, and the other is to execute the binary language program of the corresponding module directly. And the process of compiling modules into binary language programs is calledbyte compilation, this process will produce a. pyc file that corresponds to the compiled module;the. pyc file is a binary file of the compiled module;  2, byte compilation and compilation of the difference:The process of compiling a module into a binary language program is called Byte compilation, and Python is an interpreted language rather than a compiled language;a lot of people think that Python is a compiled language because of the byte compilation, which is not true;Although the process of compiling is occurring in Python, the process of compiling in Python occurs in the Python interpreter; in other words, actuallyEach program wants the computer to execute, it can only become a binary form, a compiled language refers to the software is a separate compilation module to compile the program, andThe function of byte compilation in Python is done by the interpreter, so Python is still an interpreted language。  3. Use of. PYc Files:The. pyc file is the binary file corresponding to the compiled module; The most important function of the. pyc file is to speed up the operation of the module; In addition, he can do anti-compilation and other advanced functions;viewing the. pyc file requires a binary file reader; 6-3, From...import detailed 1, learn to use From...importThe method of importing a module in Python can use import, but import only imports the module, and does not import a property or method of our module, and we not only import a module, but also a corresponding function in the import module, we can use the From ... Import statement: But From...importonly one function can be imported into the module at a timeToImport all functions from one module at a time, you can useFrom...import * 6-4. Recognize __name__ Properties 1. Understanding the main moduleIn Python, if a function calls other functions to complete a function, we call this function the main function, if a function does not call other functions, this function is called the non-main function, and the module is similar, if a module is directly used, and is not called by others, then call this module main module If a module is called by others, we call this module a non-master module; 2. Recognize _name_ propertiesIn Python there are main and non-master modules, so how to distinguish between the main module and the non-master module? So, if the value of the __name__ property of a module is __main__, then the module is the main module, and vice versa; in fact, we can think of __name__ as a variable, the variable is given by the system, the function of this variable is to determine whether a module is the main module; 6-5. Custom Modules 1, the creation and use of custom modules: Way One:Create a new file in the Lib directory, end with a. Py, this becomes a module Way Two:As long as the Python program we write to save to the Lib directory, this program is a module, the function of the module is decided by itself; 2, how to pass parameters to the module inside? The first is to ensure that the module itself does not appear in the logic and grammar errors, and how to ensure that the module does not appear in the logic and grammar errors? Method 1: By initializing the variables in the module, Method 2: Declare the type of the variable in the module first; 6-6. Dir functionThere are a lot of modules in Python, but sometimes we forget what a module does, and this time weYou can use the Dir () function to view a list of the functions of a specified module。For example:We want to see what functions are in the SYS module, so weImport the SYS module first, and then usedir (SYS)You can view all the functions and methods of the SYS module: if we want to continue to view the__doc__ MethodNote that we can continue to use thesys.__doc__the Dir () function in Python not only looks at the function list of the module, but also the list of functions for any given object: such as:Define a list d=[], and then use Dir (d) to view the features in the list:

6. 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.