Python Learning: Modules (module)

Source: Internet
Author: User

In order to write maintainable code, we grouped many functions into separate files so that each file contained relatively few code, and many programming languages used this way of organizing code. In Python, a. py file is called a module.

Why use a module?

Similar to the C language:

1, greatly improve the maintainability of the Code;

2, very versatile code can be reused, can be referenced by a third party;

3, different modules have different namespaces, you can avoid the function name, variable name conflict.


What if a different person writes the same module name? To avoid conflicts, Python introduces a way to organize modules by directory as a package.

For example, abc.py and xyz.py are modules ABC and XYZ respectively. These two module names now conflict with other modules. The solution is to design a top-level package name, such as MyCompany, to be stored in the directory:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/95/EB/wKiom1kbBpjyqDrlAAHMcDsMank709.png-wh_500x0-wm_ 3-wmp_4-s_3316263160.png "title=" screen Shot 2017-05-17 at 12.00.27 am.png "alt=" Wkiom1kbbpjyqdrlaahmcdsmank709.png-wh_50 "/>


After the package is introduced, as long as the top-level package mycompany does not conflict with others, and all modules do not conflict with others. Now abc.py module becomes MYCOMPANY.ABC module, similar to xyz.py module becomes MYCOMPANY.XYZ module.

Please note: Each package directory will be available, and there must be a __init__.py file. Otherwise python will put this directory in the original directory, not a package. __init__.py can be an empty file, or it can have code, because itself is __init__.py a module, the module is named mycompany.

You can also have multilevel catalogs that organize multiple layers of package structure:

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/95/EB/wKioL1kbBoChCcYyAAHUb7hFRj4564.png-wh_500x0-wm_ 3-wmp_4-s_3372482725.png "title=" screen Shot 2017-05-17 at 12.00.11 am.png "alt=" Wkiol1kbbochccyyaahub7hfrj4564.png-wh_50 "/>

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

when you create a module, you should be careful about naming it and not conflict with the module name that comes with Python. For example, the system comes with asysmodule, your own module is not namedsys.py, or you will not be able to import the system's ownsysmodule.

mycompany.webis also a module, please indicate the module corresponding to the. py file.


Python Learning: Modules (module)

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.