Python—11 module

In the process of computer program development, as more and more program code is written, the code in a file will become longer and longer, more and more difficult to maintain.

In order to write maintainable code, we grouped many functions into separate files, so that each file contained relatively little code, and many programming languages used this method of organizing code. In Python, a.Py file is called a module (Module).).

What are the advantages of using modules?

The biggest benefit is greatly improving the maintainability of the code. Second, writing code does not need to start from scratch. When a module is written, it can be quoted elsewhere.

When we write programs, we often refer to other modules, includingPythonBuilt in modules and modules from third parties.

Using modules can also avoid conflicts between function names and variable names. Functions and variables with the same name can all exist in different modules, so we don’t have to worry about names clashing with other modules when we write our own modules. But also be careful not to conflict with the name of built-in functions.

PythonAll built-in functions:

You may also think that if different people write the same module names, what do they do? To avoid module name collisions, Python introduces a method of organizing modules by directory called Packages.

For example, one.abc.pyThe file is a name.abcModule, onexyz.pyThe file is a name.xyzThe module.

Now, suppose we do.abcandxyzThese two module names collide with other modules, so we can organize modules through packages to avoid collisions. The method is to select a top-level package name, for example.mycompany,Store as follows:

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

After introducing packages, all modules will not conflict with others as long as the top-level package name does not conflict with others. Now,abc.pyThe name of the module becomes.mycompany.abc,Allied,xyz.pyThe module name becomesmycompany.xyz

Please note that,There will be one under each package directory.__init__.pyA document that must exist.,Otherwise, Python treats this directory as an ordinary directory, not a package.__init__.pyIt can be an empty file or Python code, because__init__.pyItself is a module, and its module name ismycompany

Similarly, a multi-level directory can be constructed to 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 documentsutils.pyThe module names aremycompany.utilsandmycompany.web.utils

!!! When creating a module, you should pay attention to naming, and you can’t conflict with the module name that Python brings. For example, if the system has its own sys module, its own module can not be named sys.py, otherwise it will not be able to import the system’s own sys module.

Summary:

When creating your own modules, we should pay attention to:

  • The module name should follow the Python variable naming specification, and do not use Chinese, special characters, or numbers.
  • Rather than conflicting with the system module name, it’s best to see if the system already has the module and check that it is executed in a Python interactive environmentimport abc,If successful, it indicates that the module exists.

 

Leave a Reply

Your email address will not be published. Required fields are marked *