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.py
The file is a name.abc
Module, onexyz.py
The file is a name.xyz
The module.
Now, suppose we do.abc
andxyz
These 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.py
The name of the module becomes.mycompany.abc
,Allied,xyz.py
The module name becomesmycompany.xyz
。
Please note that,There will be one under each package directory.__init__.py
A document that must exist.,Otherwise, Python treats this directory as an ordinary directory, not a package.__init__.py
It can be an empty file or Python code, because__init__.py
Itself 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.py
The module name ismycompany.web.www
,Two documentsutils.py
The module names aremycompany.utils
andmycompany.web.utils
。
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 environment
import abc
,If successful, it indicates that the module exists.