Python study Note 5: module and package, python study note Module

Source: Internet
Author: User

Python study Note 5: module and package, python study note Module
I. import modules using import

Cal. py:

#! /Usr/bin/pythondef add (x, y): return x + yif _ name _ = '_ main _': print add (1, 2)

Note: __name _ is a built-in variable. If the call value in CLI is _ mail __, otherwise it is a file name.

Import in new. py:

Import calprint cal. add (2, 3 );
Ii. Package: modules organized by directory name

1. Create a folder named "package name"

2. Create a _ init _. py file in the folder.

3. Store script files, compiled extensions, and sub-packages in this folder as needed

4. import pack. m1, pack. m2, and pack. m3 in the file.

5. Use pack. m1.xxx to call the module method in the package.

Sample directory structure:

Util/── cal. py ── cal. pyc ├ ── _ init _. py ── _ init _. pyc

Call example new. py:

Import util. calprint util. cal. add (2, 3); // 5

Or use the alias:

Import util. cal as cprint c. add (2, 3); // 5

You can also use the from statement to describe it:

From util. cal import addprint add (2, 3 );

 

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.