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__].
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:
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!