Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of if _ _ name__ = ='_ _ main__' in Python code

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "if _ _ name__ = ='_ _ main__' in Python code". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

If _ _ name__ = ='_ _ main__' action in Python

To figure this out, you should know that Python code files with the suffix py can be used in two ways, the first is to run them directly, and the other is to import them as modules by other py files.

When running itself directly in the first way, the value of _ _ name__ is _ _ main__

When imported by another module in the second way, its _ _ name__ value is its file name (usually referred to as the module name).

Examples are as follows:

After creating a new Python project with PyCharm, create two new py files named AAA.py and BBB.py

Then write the following code in AAA.py:

Print ('the value of _ _ name__ in the current AAA.py is:', _ _ name__)

Write the following code in BBB.py:

Import AAA

Then run the two Python files separately, and the results are as follows:

The result of running AAA.py is as follows:

The result of running BBB.py is as follows:

In addition to confirming the two ways of using the py file mentioned above, this example also shows that when you import a py file / module using import, all the code that is not indented in the imported py file will be executed.

This raises a question. What should I do if I think the code doesn't want to be executed when I import the module AAA.py? The statement "if _ _ name__ = ='_ _ main__'" can achieve this effect. How to use it? some friends should already understand the above example. If you don't understand, please see the following example:

Modify the above AAA.py code to the following code:

Print ('\ n') print ('the value of _ _ name__ in the current AAA.py is' {}'. Format (_ _ name__)) if _ _ name__ = ='_ _ main__': print ('\ n') print ('currently running file is AAA.py') else: print ('\ n') print ('current AAA.py is imported by other py files as a module')

Run AAA.py and BBB.py again, and the results are as follows:

It can be seen that through the statement "if _ name__ ='_ main__',", we can tell whether a py file is run directly or imported by another py file as a module file, and then realize that some code is not executed through this judgment.

So what kind of code usually doesn't want to be executed? When the file is defined as a module by the author, it inevitably contains some test code, which is not expected to be executed when the module is import.

Add: the operation principle of if _ _ name__ ='_ _ main__':

Each python module (python file, that is, test.py and import_test.py here) contains the built-in variable _ _ name__, when the module is executed directly, _ _ name__ is equal to the file name (including the suffix .py); if the module import to other modules, the module's _ _ name__ is equal to the module name (excluding the suffix .py).

"_ _ main__" always refers to the name of the currently executing module (including the suffix .py). Then when the module is executed directly, the result of _ _ name__ = = 'main' is true.

For further illustration, we add print (_ _ name__) before the if _ _ name__== "_ _ main__" of the test.py script, which means that _ _ name__ is printed. The contents and results of the file are as follows:

As you can see, the value of the variable _ _ name__ at this time is "_ _ main__".

Then execute import_test.py, and the execution result is as follows:

At this point, the value of the _ _ name__ variable in test.py is test, which does not meet the condition of _ _ name__== "_ _ main__", so the subsequent code cannot be executed.

This is the end of the content of "if _ _ name__ = ='_ _ main__' in Python code". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report