Python Learning--modules

Source: Internet
Author: User
Module

Brief introduction

You've learned how to define a function once in your program and reuse the code. If you want to reuse many functions in other programs, how do you write programs? As you might have guessed, the answer is to use a module. A module is basically a file that contains all of the functions and variables you define. In order to reuse modules in other programs, the file name of the module must be an extension of. py.
Modules can be entered from other programs to take advantage of its functionality. This is also the way we use the Python standard library.
The following example shows how to use the standard library module.

Import sysprint ' The command line arguments be: ' For I in sys. argv:    Print i-    print ' \n\nthe PYTHONPATH is ', sys. path, ' \ n '

Output

The command line arguments are:d:\helloword\src\moudle\test\sysmoduletest.pythe PYTHONPATH is [' d:\\helloword\\src\\ Moudle\\test ', ' d:\\helloword\\src ', ' d:\\java\\python27\\python27.zip ', ' d:\\java\\python27\\dlls ', ' D:\\Java\\ Python27\\lib ', ' D:\\java\\python27\\lib\\plat-win ', ' d:\\java\\python27\\lib\\lib-tk ', ' D:\\Java\\Python27 ', ' D:\ \java\\python27\\lib\\site-packages ']

First, we use the import statement to enter the SYS module.
When Python executes the import SYS statement, it looks for the sys.py module in the directory listed in the Sys.path variable. If this file is found, the statement in the main block of the module will be run and the module will be able to be used by you.
The argv variable in the SYS module is indicated by the use of a dot, one of the advantages of SYS.ARGV This method is that the name does not conflict with any argv variables that are used in your program. In addition, it clearly shows that this name is part of the SYS module.
The SYS.ARGV variable is a list of strings.
Note: 1) Python starts counting from 0, not starting from 1.

2) The initialization process is only performed when we first input the module.

3) "SYS" is the abbreviation for "system".

Byte-compiled. pyc files

It is relatively time-consuming to enter a module, so Python does some tricks to make the input module faster. One method is to create a byte-compiled file with the. pyc as the extension. Byte-compiled files are related to the middle state of the Python transformation program. When you next enter this module from another program, the. pyc file is very useful because the processing required for some of the input modules is complete. In addition, the files compiled by these bytes are platform-independent.

From.. Import statement

If you want to enter the ARGV variable directly into your program (avoid playing sys every time you use it), then you can use the FROM SYS import ARGV statement. If you want to enter all the names used by the Sys module, you can use the FROM SYS import * statement. This is true for all modules. Generally, you should avoid using from. Import uses the import statement because it makes your program easier to read and avoids conflicting names.

__name__ of the module

Each module has a name that can be used in the module to find the name of the module through a statement. When a module is entered for the first time, the main block of the module will be run. What if we just want to run the main block while the program itself is being used, and not run the main block when it is entered by another module? This can be done through the module's Name property.
The __name__ method of using the module is shown in the following example

Python code

if __name__ = = ' __main__ ':

print ' This program was being run by itself '

else:

print ' I am being imported from another module '

Output

Python code

$ Python using_name.py This program was  being run by itself

$ python

>>> Import using_name  I am being imported from another  module  >>>

Each Python module has its __name__, if it is ' __main__ ', which means that the module is run by the user individually, we can do the appropriate operation.

Make your own module

Creating your own module is very simple and you've been doing it! Each Python program is also a module. You have ensured that it has a. py extension. The following example will make it clearer.
The methods for creating your own modules are shown in the following example:

1. First, we create our own module, which is nothing special compared to our normal Python program.

Python code

Def sayhi ():      print ' Hi, this is mymodule speaking. '  Version = ' 0.1 '

2. Then, see how to use this module in our other Python programs.

Python code

Import MyModule    mymodule.sayhi ()  print ' Version ', mymodule.version

Output

Python code

Hi, this is MyModule speaking.  Version 0.1

Dir () function

You can use the built-in Dir function to list the identifier for a module definition. Identifiers have functions, classes, and variables. When you provide a module name for Dir (), it returns a list of the name of the module definition. If no argument is supplied, it returns a list of names defined in the current module.

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