Python Learning Note __5 module

Source: Internet
Author: User

# This is a learning note for the Liaoche teacher Python tutorial

1. Overview

To write maintainable code, we group many functions into separate files so that each file contains relatively few code, and in Python, a. py file is called a module.

1.1 , the benefits of using modules

    1. Improved maintainability of the code

    2. When a module is written, it can be referenced elsewhere

    3. Avoid collisions of function names and variable names. Functions and variables of the same name can exist in separate modules

1.2 , python ways to organize your modules

To avoid module name collisions, Python introduces a way to organize modules by directory, called packages.

The practice of the package is:

Select a top-level package name, such as MyCompany, to be stored in the following directory:

MyCompany
├─ __init__.py
├─ abc.py
└─ xyz.py

Once the package is introduced, all modules will not conflict with others as long as the package name in the top layer does not conflict with others. Now,abc.pythe name of the module becomesMYCOMPANY.ABC"formerly called ABC", like that,xyz.pythe module name becomes themycompany.xyz"formerly calledXYZ"

__init__.py This file must exist, otherwise Python will treat the directory as a normal directory, not a package.

_ _init__.py can be an empty file, or it can have Python code, because __init__.py itself is a module, and its module name is MyCompany

Similarly, there can be multi-level catalogs, which form a multi-level package structure. For example, the following directory structure:

MyCompany
├─Web
│  ├─__init__.py
│  ├─utils.py
│  └─www.py
├─ __init__.py
├─ abc.py
└─ xyz.py

filewww.pythe module name ismycompany.web.www, two filesutils.pythe module names aremycompany.utilsand themycompany.web.utils.


Python Learning Note __5 module

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.