Home  >  Article  >  Backend Development  >  What is the python entry function?

What is the python entry function?

coldplay.xixi
coldplay.xixiOriginal
2020-10-21 11:50:399455browse

The python entry function is [if __name__ == "__main__"], [print (__name__)] prints [__main__], that is to say, when a module is called as a whole, the module name [ The value of __name__] is [__main__].

What is the python entry function?

Usage of entry function

First code

#这是一个测试函数
import time 
    
def func():
    print("666")
if __name__ == "__main__":
    print("222") 
    func()
print(__name__)
print(time.__name__)

If you execute this py file directly, the output will be 222 and 666, here you can see that if __name__ == "__main__": is executed by us as an entry.

The print result of the last two sentences of the above code is:

What is the python entry function?

print (__name__) The printed result is __main__, that is to say, when a module is called as a whole, the value of the module name .__name__ is __main__.

print(time.__name__)When a module is referenced by other modules, the output will be the module's own name. When a module is referenced by other modules, it does not An entry function is required.

The above explains well why the entry function is needed.

Related free learning recommendations: python video tutorial

The above is the detailed content of What is the python entry function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn