Python study notes (seventeen) ----- module related

Table of contents

(1) module

(2) How to import modules

①import module name

②from module name import function name

③from module name import *

④as defines an alias

(3) Custom modules

(4) Test module


(1) module

A Python module (Module) is a Python file ending with .py . A module can define functions, classes, and variables, and it can also   contain executable code.

The role of the module :  There are many different modules in python , and each module can help us quickly realize some functions , such as implementing time-related functions, you can use the time module. We can think of a module as a toolkit , and each toolkit has various tools for us to use to achieve various functions. In short, a module is a Python file, which contains classes, functions, variables, etc., which we can use (import modules to use)

(2) How to import modules

Modules need to be imported before use. The import syntax is as follows :

Precautions:

from can be omitted, just import directly
The as alias can be omitted
Use "." to determine the hierarchical relationship
The import of the module is generally written at the beginning of the code file

Common combinations such as:

import module name
from module name import classes, variables, methods, etc.
from module name import *
import module name as alias
from module name import function name as alias

import module name

Basic syntax:

Case: Import time module

from module name import function name

Basic syntax:

Case: Import the sleep method in the time module

from module name import *

Basic syntax:

 Case: Import all methods in the time module

as defines an alias

Basic syntax:

case:

(3) Custom modules

Just write the code normally in the Python code file, and use the import and from keywords as importing Python built-in modules to use. For example, create a new Python file, name it my_module1.py , and define the test function

Note: Each Python file can be used as a module, and the name of the module is the name of the file . That is to say, the custom module name must conform to the identifier naming rules

(4) Test module

In actual development, when a developer writes a module, in order to make the module achieve the desired effect in the project, the developer will add some test information to the py file, for example , in the my_module1.py file Add test code test(1,1)

Problem : At this point, whether it is the current file or other files that have imported the module, the `test` function call will be automatically executed when running

solution:

if __main__ == "__main__" means that only when the program is directly executed will it enter the if , if it is imported, the if cannot enter

Notice:

 Note: When importing multiple modules , and there is a function with the same name in the module . When this function with the same name is called, the function of the module imported later is called

 __all__

If there is a `__all__` variable in a module file, only the elements in this list can be imported when using `from xxx import *`

 

Guess you like

Origin blog.csdn.net/laosao_66/article/details/131605332