[Python] module import ⑤ (Main program judgment statement | Executing function problem in the module | Make a custom module and execute the function | Importing a custom module will execute the code in the module)





1. The problem of executing functions in modules




1. Make a custom module and execute the function


If in a custom module, a function is defined and called;

As shown in the following code:

def add(a, b):
    print("调用 my_module 模块中的功能")
    return a + b


print(add(3, 4))

Execute the my_module module, the result is as follows:

D:\001_Develop\022_Python\Python39\python.exe D:/002_Project/011_Python/HelloPython/my_module.py
调用 my_module 模块中的功能
7

Process finished with exit code 0


2. Importing a custom module will execute the code in the module


In the main code, import the custom module;

"""
自定义模块 代码示例
"""

# 导入自定义模块
import my_module

Execute the above code, the result is as follows, the function in the my_module module is not called, but the function is still triggered;

D:\001_Develop\022_Python\Python39\python.exe D:/002_Project/011_Python/HelloPython/Hello.py
调用 my_module 模块中的功能
7

Process finished with exit code 0

This is because import imports the module, copies all the code in the module to the code location at one time, and executes the code, that is, executes all the code in my_module;

insert image description here


3. Main program judgment statement


The main program judgment statement is provided in Python:

if __name__ == '__main__':

Its function is to judge whether the current module is running as an independent main program;

A module can be imported by other modules, and can also be run as an independent main program;

When a module is imported, Python will execute the code of the module, but the entry of the main program will be set to the code of the module that imported the module;

And when a module runs as an independent main program, Python will directly execute the code of the module;

To distinguish between these two cases, Python introduces a special variable __name__that varies depending on how the current module is run;

  • When a module is imported, __name__the value of is the name of the module, which if __name__ == '__main__'is not true at this time;
  • When a module is run as an independent main program, __name__ the value of is established __main__at this time if __name__ == '__main__';


4. Code example - main program judgment statement


Put the executable code in the module in if __name__ == '__main__':the code block,

Only when you right-click the "Run" option, __name__the value is __main__, the code block will be triggered to execute;

insert image description here

Execute again at this time

"""
自定义模块 代码示例
"""

# 导入自定义模块
import my_module

The main code, the execution result is, does not trigger the execution of the executable code in the module;

D:\001_Develop\022_Python\Python39\python.exe D:/002_Project/011_Python/HelloPython/Hello.py

Process finished with exit code 0

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/131398906