“This is my 27th day of participating in the First Challenge 2022. For more details: First Challenge 2022.”
Official Python column # 76, stop! Don’t miss this zero-based article!
In the previous article, the committee shared a process pool, which specifically stated that other modules must be called within or within the ‘__main__’ code block to create a process pool, otherwise an error will be reported.
So what’s behind it? I think it can be divided into two articles to explain clearly.
In this separate article, we’ll look at the built-in __name__ attribute.
The first point is that executable code in a script/module must be executed when loaded
Strictly speaking, a ‘__main__’ code block is not an entry point to a script, since Python programs are executed line by line from the top line of the module. So executable code (expressions or function calls, etc.) is executed as soon as it is loaded by the interpreter, never waiting to be put into a ‘__main__’ block.
This is the first point, and it’s how the Python compiler works. If more explanation is needed, the committee will share the source code exhibition around running the Python compiler later, but it is outside the scope of this article.
Second, when actively loading a module, the module’s __name__ attribute is set to ‘__main__’.
When the Python interpreter runs an entire script/module directly, it sets the module’s __name__ attribute (built-in variable) to ‘__main__’.
Here said ‘active loading’ is relative, in order to explain the convenience of the concept put forward by the committee, more accurately, the implementation of the running module. A Python module is a collection of code that is inherently non-dynamic, the reader must understand.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2022/2/27 10:53 PM
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : lib01.py
# @Project : DeepDivePython
def demo() :
print("demo in lib:", __name__)
print("leixuewei")
demo()
if __name__ == "__main__":
print("execute in main block")
Copy the code
As shown in the figure below, we see the path > using: venv/bin/python <lib01.py\, which essentially runs the Python script with the compiler.
Here the module’s lib01._name_ property changes to ‘__main__’ and finally prints’ execute in main block ‘.
Third, when a module is passively loaded, the value of the built-in attribute __name__ property becomes the module name
When we import a module, the code inside the ‘__main__’ block will not be run, and the executable code inside the other modules will continue to be loaded and executed as usual.
As shown in the code below
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @time: 2022/2/27 10:55 PM
# @Author : LeiXueWei
# @csDN /Juejin/Wechat: Lei Xuewei
# @XueWeiTag: CodingDemo
# @File : call_lib01.py
# @Project : DeepDivePython
import lib01
lib01.demo()
Copy the code
As shown in the figure below, there are three printed sentences, the first two of which are the output explaining the execution while loading lib01.
The third sentence is the output of the lib01.demo() call to call_lib01.py.
Lib01’s built-in __name__ attribute is now ‘lib01’.
Most of the demo code shared by the previous committee is executed by calling methods directly from within the script.
conclusion
This article shares 3 points, hope readers can understand, understand! This is the foundation of Python programming.
For those who like Python, please check out the Python Basics section or the Python Getting Started to Master Section
Continuous learning and continuous development, I am Lei Xuewei! Programming is fun. The key is to get the technology right. Welcome to wechat, like support collection!