Python Learning note five: modules and packages

Source: Internet
Author: User

First, the module imports with import

cal.py:

# !/usr/bin/python def Add (x, y    ): return x+yif__name__'__main__':     Print Add (+)

Note: __name__ is a built-in variable and is a file name if the value is called __mail__ directly in the CLI.

Import in new.py:

Import Cal print cal.add (2,3);
II. Packages: Modules organized by directory name

1. Create a folder with the name of the package name

2. Create a __init__.py file under this folder

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

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

5, using Pack.m1.xxx to invoke the module in the package method

Sample directory Structure:

util/__init____init__. PYc

Call Example new.py:

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

or using as to use aliases:

Import util.cal as C print c.add (2,3); 5

Or use the From to shorthand:

 from Import Add Print Add (2,3);

Python Learning note five: modules and packages

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.