How to use the entry function in Python

Source: Internet
Author: User

Usage of the entry function

First on the code

# This is a test function Import Time      def func ():     Print ("666") if __name__ " __main__ " :     Print ("222")     func ()

Print (__name__)
Print (time.__name__)

 

If you execute the PY file directly, the output is 222 and 666, and here you can see if __name__ = = "__main__": is executed as a portal by us.

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

Print (__name__) prints the __main__, which means that 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 need an entry function itself.

It's a good explanation of why you need an entry function.

How to use the entry function in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.