"Python" Learning Note 12: Modules

Source: Internet
Author: User

Modules (module)

In Python, a. py file is a module. Through the module, you can invoke programs in other files

Introducing Modules

Write a first.py file that reads as follows:

def Letter ():    print(' ABC ')


Write a second.py file that reads as follows:

Import firstfor i in Range (    ): First.letter ()

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


Once the module is introduced, we can invoke an object in the module by means of the module. Object

How to introduce modules in Python

Import a as B # introduces module a renamed to B

From a import function1 # A Function1 object is introduced from module A, and when you call an object in a, we don't use the a.function1 directly with Function1.

From a import * #调用时直接使用对象 instead of a. Object

The following three formulations are the same

Notation 1

import first as Afor i in range:    a.letter ()

Notation 2

Import letterfor i in range (+): Letter    ()

Notation 3

Import *for i in range (+): Letter    ()
Module Package

We can put files of similar function in a folder (for example: Exp_dir), constitute a module package, through

import exp_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.


"Python" Learning Note 12: Modules

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.