Introduction to the Python Advanced Tutorial modules (module) _python

Source: Internet
Author: User
Tags in python

We saw the functions and objects before. In essence, they are all for the better organization of existing procedures to facilitate reuse.

Modules (module) are also intended for the same purpose. In Python, a. py file forms a module. Through modules, you can invoke programs in other files.

Introducing Modules

We first write a first.py file, which reads as follows:

Copy Code code as follows:

Def laugh ():
print ' Hahahaha '

Write a second.py and introduce the program in the:

Copy Code code as follows:

Import a

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


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

After the module is introduced, an object in the module can be invoked by means of a module. In the example above, the laugh () is the object introduced by the introduction of the module.

There are other ways to introduce Python

Copy Code code as follows:

Import a as B # to introduce module A and rename module A to B

From a import function1 # introduces the Function1 object from module A. When we call an object in a, we don't need to explain the module, which is directly using function1 instead of A.function1.

From a import * # introduce all objects from module A. When we call an object in a, we don't need to explain the module, which is to use the object directly, not the A. Object.


These reference methods can be easily written by the following programs.

Search Path

Python searches the following path for the module it wants to find:

The folder where the program resides
Installation path for the standard library
The path that the operating system environment variable Pythonpath contains

If you have a custom module, or a downloaded module, you can put it in the appropriate path according to the situation so that Python can find it.

Module Pack

Modules with similar functions can be placed in the same folder (for example, This_dir) to form a modular package. Pass

Copy Code code as follows:

Import This_dir.module

Introduce module modules in the This_dir folder.

The folder must contain a __init__.py file that reminds Python that the folder is a module package. __init__.py can be an empty file.

Summarize

Import Module

Module.object

__init__.py

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.