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 and principle of if _ _ name__ = & # 039 in Python programming

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the role and principle of if __name__ == 'main' in Python programming". In daily operation, I believe many people have doubts about the role and principle of if __name__== 'main' in Python programming. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer your doubts about "what is the role and principle of if __name__== 'main' in Python programming"! Next, please follow the small series to learn together!

__name__is the name of the current module, which is__main__when the module is run directly. This means that the following code blocks will be run when the module is run directly, and not when the module is imported.

I believe that beginners in the process of learning Python, inevitably will always encounter if __name__= 'main' statement, although I always know its role, but may have been more vague, today rookie analysis with everyone for example to talk about my understanding.

For example, we write the following code in test.py:

print ('Learn Python') def main(): print ('Learn Python') if __name__ == '__main__': main() print ('Follow the rookie analysis, practice Python more practice more love')

Run the code and the output is:

Then, create a new script named import_test.py in the same folder and simply type in the code:

import test

Run the code and the output is:

Compared to the previous test.py code run, only the output is Python, that is, if __name__=="__main__": the statements before are executed, and the statements after are not executed.

Why is that? Don't worry, rookie analysis continues to give you examples to illustrate, absolutely let you satisfied until, finally have a kind of suddenly enlightened feeling.

Now, we add print __name__before if __name__=="__main__": in the test.py script, that is,__name__is printed, and the final result is as follows:

As you can see, the variable__name__now has the value "__main__."

Then execute import_test.py, and the module contents and execution results are as follows:

In this case, the__name__variable value in import_test.py is test, which does not satisfy the condition of__name__=="__main__", so the following code cannot be executed.

Haha, is it understood at once, feel good, give rookie analysis point praise oh!

If you think about it carefully, the principle of its operation is:

Since every Python module (Python file) contains a built-in variable__name__,__name__equals the filename (including the suffix.py) when the module is executed. If imported into another module,__name__equals the module name (without the suffix.py). And "__main__" equals the name of the current execution file (including the suffix.py). So__name__== '__main__' is true when the module is executed directly, and false when the module is imported into another module, i.e., the corresponding method is not called.

In short: __name__is the name of the current module and__main__when the module is run directly. Code will be run when modules are run directly, code will not be run when modules are imported.

At this point, the study of "what is the role and principle of if __name__ == 'main' in Python programming" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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