What is a module in Python

Yesterday a fan asked me, what is a module ? I often hear this word mentioned in other people's mouths, but I just don't understand it.

insert image description here

A module can be thought of as a box of themed building blocks through which something of a certain theme can be spelled out. This is different from the function introduced before, a function is equivalent to a building block, andA module can contain many functions. As shown below:

insert image description here

In Python, a file with the extension " .py " is called a module. For example, the image is a module.

insert image description here

Normally, we will be able toimplement a specific functionthe codeplaced in a fileAs a module , just like the above module, it realizes the function of collecting pictures in batches , and it contains several functions .

The benefits of using modules in programming are as follows:

  1. Avoid conflicts such as function names and variable names . In the same Python file, if the same variable name, function name, etc. are used, it is easy to cause code errors or confusion in execution results; but if the same variable name, function name, etc. appear in different Python files, they will not affect each other.

  2. Improve the maintainability of the code . The code is divided into multiple different files according to different functions, and the later maintenance is convenient for quick search.

  3. Improve code reusability . That is, after writing a module, as long as it is a program that realizes the function, it can be imported into this module to realize it.

Guess you like

Origin blog.csdn.net/2201_75641637/article/details/129641361