current position:Home>When writing Python scripts, be sure to add this
When writing Python scripts, be sure to add this
2022-01-30 18:52:52 【somenzz】
I found that many friends wrote Python The script is very casual , Or no function , Or functions are defined everywhere , Anyway, I can't see where the first line of code to be executed is at first sight , Such scripts are poorly readable , And easy to hide bug, in addition , This is not Python Community recommended practices , It's easy to solve this problem , When we write Python Script time , Make sure to add this :
def main():
# do something
print("do something.")
if __name__ == "__main__":
main()
Copy code
You may object : I can write as much as I like , Why listen to you , Write more if __name__...
?
Don't worry. , Let me say three reasons .
First of all , It makes Python The role of the document is more clear
First of all, understand __name__
The role of , When the script is directly Python When the interpreter executes , Its value is "main", When it is used by others Python Program import When , Its value is the corresponding Python Script name , Can be in Python The interpreter verifies , Suppose there is a some_script.py It reads as follows :
print("some_script.py")
print(__name__)
Copy code
stay Python The interpreter imports :
* vim some_script.py
* python
Python 3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import some_script
some_script.py
some_script
>>>
Copy code
You can see ,__name__
The value is Python The filename of the script some_script.
in other words if __name__ == "__main__":
The following code is import It won't run when .
I understand that ,if __name__ == "__main__":
It can be used as a flag to distinguish between scripts and Libraries , When we see if __name__ == "__main__":
when , I think this is a script that can be run directly , When you don't see this line of code , I think this is a library , Can be referenced by other programs ,Explicit is better than implicit.
, isn't it? ?
Another example :
If you write one without if __name__ == "__main__":
Script for , It's called bad_script.py, The contents are as follows :
def useful_function(x):
return x * x
class UsefulClass:
def __init__(self, x):
self.x = x
# You tested it yourself , No problem
for i in range(7):
print(useful_function(i))
Copy code
Someone else wrote useful.py, Quoted your useful_function:
from bad_script import useful_function
def main():
print(f'{useful_function(3)=}')
if __name__ == '__main__':
main()
Copy code
I. operation , Unexpected content was found printed , See the red part in the figure below :
For a long time , It was found that it was the output of your script , Do you think others will scold you ?
second , It makes Python Documents are easier to read , Yes IDE friendly
With if __name__ == "__main__":
amount to Python The program also has an entry function , We can clearly know where the logic of the program begins ( Of course, we also need to consciously put the starting logic of the program here )
Actually , This is also PyCharm Recommended Practice , When you create a new project , It creates by default main.py It's like this :
stay if __name__ == "__main__":
There is also a Green Run button on the far left of the line , Click on it. , The program starts from this line .
Why are many good programming languages , such as C、Java、Golang、C++ There is one. main The entry function ? I think a very important reason is that the program entry is unified , Easy to read .
Third 、 In the multi process scenario , Must use if main
For example, you do parallel computing with multiple processes , Wrote such code :
import multiprocessing as mp
def useful_function(x):
return x * x
print("processing in parallel")
with mp.Pool() as p:
results = p.map(useful_function, [1, 2, 3, 4])
print(results)
Copy code
When you run , You will find that the program keeps creating processes , At the same time, they are constantly reporting errors RuntimeError, Even if you Ctrl C You can't terminate the program . And added if __name__ == "__main__":
The program will proceed as expected :
import multiprocessing as mp
def useful_function(x):
return x * x
if __name__ == '__main__':
print("processing in parallel")
with mp.Pool() as p:
results = p.map(useful_function, [1, 2, 3, 4])
print(results)
Copy code
Why is that ?
In fact, I understand Hu ,Python Multiple programs start multiple Python Resolver , Every Python The interpreter will import your script , Copy a copy of global variables and functions to the child process , If you have the if __name__ == "__main__":
, Then the code behind it will not be import, It will not be repeated . otherwise , The code that creates multiple processes will be import, It will be executed , So as to create sub processes infinitely recursively ,Python3 Will be submitted to the RuntimeError, The order is to create the process first , Then report the wrong , So there will be a constant creation process , Keep reporting mistakes ,Ctrl C Can't stop the phenomenon , Can only kill Drop the whole terminal . Here is a Official explanation
Last words
if __name__ == "__main__":
Although not mandatory , But for these three reasons , I strongly recommend that you do this , It is Python Community agreement , Corresponding Python zen : Clarity is better than obscurity . just as _
As a variable name, it means to tell the person reading the code : This variable is not important , I won't use it later . When you see Python The script has if __name__ == "__main__":
when , You realize that , This is an executable script , When imported by other programs , This part of the code will not be executed , In a multi process program , It's a must .
If there is a harvest , Welcome to thumb up 、 Focus on 、 Looking at , Thank you for reading .
copyright notice
author[somenzz],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301852501644.html
The sidebar is recommended
- Exploratory data analysis (EDA) in Python using SQL and Seaborn (SNS).
- Turn audio into shareable video with Python and ffmpeg
- Using rbind in python (equivalent to R)
- Pandas: how to create an empty data frame with column names
- Talk about quantifying investment using Python
- Python, image restoration in opencv - CV2 inpaint
- Python notes (14): advanced technologies such as object-oriented programming
- Python notes (13): operations such as object-oriented programming
- Python notes (12): inheritance such as object-oriented programming
- Chapter 2: Fundamentals of python-5 Boolean
guess what you like
-
Python notes (11): encapsulation such as object-oriented programming
-
Python notes (10): concepts such as object-oriented programming
-
Gradient lifting method and its implementation in Python
-
Van * Python | simple crawling of a site course
-
Chapter 1 preliminary knowledge of pandas (list derivation and conditional assignment, anonymous function and map method, zip object and enumerate method, NP basis)
-
Nanny tutorial! Build VIM into an IDE (Python)
-
Fourier transform of Python OpenCV image processing, lesson 52
-
Introduction to python (III) network request and analysis
-
China Merchants Bank credit card number recognition project (Part I), python OpenCV image processing journey, Part 53
-
Introduction to python (IV) dynamic web page analysis and capture
Random recommended
- Python practice - capture 58 rental information and store it in MySQL database
- leetcode 119. Pascal's Triangle II(python)
- leetcode 31. Next Permutation(python)
- [algorithm learning] 807 Maintain the city skyline (Java / C / C + + / Python / go / trust)
- The rich woman's best friend asked me to write her a Taobao double 11 rush purchase script in Python, which can only be arranged
- Glom module of Python data analysis module (1)
- Python crawler actual combat, requests module, python realizes the full set of skin to capture the glory of the king
- Summarize some common mistakes of novices in Python development
- Python libraries you may not know
- [Python crawler] detailed explanation of selenium from introduction to actual combat [2]
- This is what you should do to quickly create a list in Python
- On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
- Python OpenCV image area contour mark, which can be used to frame various small notes
- How to set up an asgi Django application with Postgres, nginx and uvicorn on Ubuntu 20.04
- Initial Python tuple
- Introduction to Python urllib module
- Advanced Python Basics: from functions to advanced magic methods
- Python Foundation: data structure summary
- Python Basics: from variables to exception handling
- Python notes (22): time module and calendar module
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
- Python notes (22): errors and exceptions
- Python has been hidden for ten years, and once image recognition is heard all over the world
- Python notes (21): random number module
- Python notes (19): anonymous functions
- Use Python and OpenCV to calculate and draw two-dimensional histogram
- Python, Hough circle transformation in opencv
- A library for reading and writing markdown in Python: mdutils
- Datetime of Python time operation (Part I)
- The most useful decorator in the python standard library
- Python iterators and generators