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

The confusing if __name__== __main__ in Python

編輯:Python

In Python code, we often see:

if __name__ == '__main__':

A simple explanation of the usage of this statement is: ifif __name__== '__main__'The module where it is located is run directly, then the code block under this statement is run, if the module it is located in is imported into otherrun in a python script, the code block under this statement will not be run.

1. Program entry

For many programming languages, programs must have an entry, such as C, C++, and fully object-oriented programming languages ​​Java, C#, etc.Among them, both C and C++ need a main function as the entry point of the program, that is, the operation of the program will start from the main function.Similarly, Java and C# must have a main class with a main method as the program entry.

However, unlike C, C++, Java, and C#, Python is a scripting language. Unlike compiled languages, the program is first compiled into binary and then run. Python is dynamically interpreted line by line, that is,It runs from the first line of the script, and there is no unified entry.

Also we know that there are two ways to use Python files:

The first: execute directly as a script
The second: import into other Python scripts to be called&#x


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