程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Understand if in Python__ name__ == __ main__

編輯:Python

1. introduction

stay Python In the related code , We often encounter the following code snippet :

# stuff
if __name__ == "__main__":
# do stuff

This article will try to use as simple examples as possible to explain what happened here , And the need to use if __name__=="__main__" The circumstances of . Please note that , In the above code name and main Before and after 2 Underscore characters .
Gossip , Let's start straight !

2. Special variables

When we run our Python Script time , The variables in this script __name__ The value of is usually __main__. Let's look at an example :

# first.py
print(__name__)

We run the above script in the terminal , The code is as follows :

python first.py

Output is as follows :

__main__

Observe the above output , When we run the script first.py When , Variable __name__ The value of is set to __main__.

3. Complex examples

The above example is relatively simple , It's also relatively easy to understand . Next, let's take an example with multiple files , Suppose we have three scripts , Respectively a.py,b.py as well as c.py.
among ,a.py Is as follows :

# a.py
print("__name__ in a.py:", __name__)
from b import *
from c import *

here ,b.py Is as follows :

# b.py
print("__name__ in b.py:", __name__)

c.py Is as follows :

# c.py
print("__name__ in c.py:", __name__)

If we run the command python a.py The results are as follows :

__name__ in a.py: __main__
__name__ in b.py: b
__name__ in c.py: c

Notice in the script a.py We see __name__ The value of is __main__, At this point in the script b.py in __name__ The value of is b, meanwhile c.py in __name__ The value of is c. This is because the script we run is a.py, Then the script is a.py Medium variable __name__ The value of will be set to string __main__.

Besides , Script b.py and c.py The variables in the ·__name__ Keep as b and c, This is because the script b.py and c.py Not a direct script . It's the script we run a.py Called b.py and c.py Medium print function .

If we run alone python b.py The results are as follows :

__name__ in b.py: __main__

Accordingly, we run python c.py, The results are as follows :

__name__ in c.py: __main__

Observe the above two outputs , When we run the script directly b.py or c.py, The variables in the corresponding script __name__ Will be set to __main__.

4. Use scenarios

As Python developer , We usually create multiple projects in one project Python File instead of writing all the code in one Python In file . So we usually have a master Python The file is used as the entry of the project , At the same time, other files contain some auxiliary function implementations .
Let's look at an example , Suppose we have two Python file , as follows :

# main.py
from helper import *
print(greet("bob"))

Auxiliary function implementation helper.py The contents are as follows :

# helper.py
def greet(name):
return "hello " + name
print(greet("testname"))

If we run the command at this time python main.py, The results are as follows :

hello bob
hello testname

Mainly the above import Statement will be from helper.py Introduce all statements in , therefore helper.py Test statements in print(greet("testname")) Will also be implemented . Although we can do this by adding comments or masking comments main.py or helper.py To control the output of the test statement , But this situation is the statement
if __name__=="__main__" The situation of showing one's skill .

5. Solution

In view of the above situation , Code rectification , as follows :

# main.py
from helper import *
print(greet("bob"))

We modify the file helper.py The contents are as follows :

def greet(name):
return "hello " + name
if __name__ == "__main__":
print(greet("testname"))

here , We carry out orders python main.py, The results are as follows :

hello bob

Be careful , here helper.py The variables in the __name__ The value of is helper, therefore helper.py Medium if The statement doesn't hold , In turn, our test statements will not be executed print(greet("testname")).

here , If we run the command alone python helper.py , The results are as follows :

hello testname

Separate operation helper.py when , At this point, change the variables in the script __name__ Will be set to __main__, The test will be executed at this time , Output the corresponding test results .

6. summary

This article focuses on the introduction Python Common in if __name__=="__main__" , It focuses on the analysis of the principle behind it and the application scenario .

You've learned to waste ?

Official account 《AI The way of algorithm 》, For more AI Algorithm information .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved