Python advanced tutorial module introduction, python advanced tutorial

Source: Internet
Author: User

Python advanced tutorial module introduction, python advanced tutorial

We have seen functions and objects before. Essentially, they all aim to better organize existing programs for reuse.

The module is also for the same purpose. In Python, A. py file forms a module. Through the module, you can call programs in other files.

Introduction module

First, write a first. py file with the following content:
Copy codeThe Code is as follows:
Def laugh ():
Print 'hahahahaha'

Write another second. py and introduce the program in first:
Copy codeThe Code is as follows:
Import first

For I in range (10 ):
First. laugh ()

In second. py, we use the laugh () function defined in first. py.

After a module is introduced, an object in the introduced module can be called through the module. Object method. In the above example, the first is the introduced module, and laugh () is the object we introduce.

There are other import methods in Python.
Copy codeThe Code is as follows:
Import a as B # introduce module a and rename module a to module B

From a import function1 # introduce the function1 object from module. When calling an object in a, we do not need to describe the module, that is, directly use function1 instead of a. function1.

From a import * # introduce all objects from module. When calling an object in a, we do not need to describe the module, that is, directly use an object instead of a. object.

These Reference Methods facilitate subsequent program writing.

Search Path

Python will search for the module it is looking for in the following path:

Folder where the program is located
Standard Library installation path
Path included in the operating system environment variable PYTHONPATH

If you have a custom module or a downloaded module, you can put it in the corresponding path as needed so that Python can find it.

Module package

You can place modules with similar functions in the same folder (such as this_dir) to form a module package. Pass
Copy codeThe Code is as follows:
Import this_dir.module

Introduce the module in the this_dir folder.

This folder must contain a _ init _. py file to remind Python that the folder is a module package. _ Init _. py can be an empty file.

Summary

Import module

Module. object

_ Init _. py


For python module

No module has different usage and functions. If you want to learn Python, you can start from the basic library. If you want to complete any project, you can focus on that module. Why download all modules?
Sourceforge.net/
Almost all Python modules can be found on this website.

How to create and use the Python Module

Create. py file, which is placed in the directory in pythonpath, such as the same directory (the directory must contain _ init __. py, empty files can also be), other programs can import it, as for what to import, you write it yourself.
Import directly
Import Module name
If you write it yourself, there is no special case that it is the name of the py file, which can be the directory name.

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.