Understanding of if __name__ == "__main__":

(Based on the understanding of a big brother)

For example, there is the following code in client.py

if __name__ == "__main__":
    target = "127.0.0.1"
    port = 4444
    main(target, port)

Think of client.py as a person: Xiao Ming,
if you need him to help you, you are looking for Xiao Ming.
For you, Xiao Ming is Xiao Ming : name= "client.py"
Xiao Ming is looking for himself when he is working for himself
For Xiao Ming, Xiao Ming is me : name="main"

That is to say,
when it is imported, the code below name is not main will not run,
but when client.py is run directly, the name is main, if name == " main ": the following code will run

Guess you like

Origin blog.csdn.net/qq_25504829/article/details/104816302