Introduction to Python modules

During the development process, as more program code is written, the code in a file will become longer and longer, making it increasingly difficult to maintain.

In order to write maintainable code, we group many functions and put them into different files. In this way, each file contains relatively less code. Many programming languages ​​​​adopt this organization of code. Way. In Python, a .py file is called a module.

We have studied functions before and know that a function is a program that implements one or more functions. In fact, modules are extensions of function functions. Why do you say that? That's because modules are actually blocks of programs that implement one or more functions.

Through the above definition, it is not difficult to find that both functions and modules are used to implement functions, but the scope of modules is wider than functions. In a module, there can be multiple functions.

Now that we understand what a module is, why do we need a module? Now that there are functions, why do we need modules?

The biggest benefit is that it greatly improves the maintainability of the code. Second, you don't have to start from scratch when writing code. When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including Python's built-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 exist in different modules. Therefore, when we write the module ourselves, we do not have to consider that the name will conflict with other modules. But also be careful not to conflict with built-in function names.

Python itself has many very useful modules built in. As long as they are installed, these modules can be used immediately. We can try to find these modules. For example, my Python installation directory is the default installation directory, which is C:\Users\Administrator\AppData\Local\Programs\Python\Python36, and then find the Lib directory, and you will find that it is all Modules, yes, these .py files are modules.

c875fbc1551209031bfef3995d06b41.png

In fact, modules can be divided into standard library modules and custom modules, and the Lib directory we just saw are all standard library modules.

Continuing Learning
||
submitReset Code