Python if __name__ = = ' __main__ ' What's hell is it?

Source: Internet
Author: User
Python if __name__ = = ' __main__ ' What's hell is it?

Python believes that all modules can be executed or import

If a module is imported by import, the caller of the module may just want to use part of the module's code instead of running it directly

If the module is running directly, like the C or Java language, you need a portal main

In order to solve this problem, the Python convention, when the module is executed, the value of __name__ is __main__, otherwise, the value of the module's name

And if __name__ = = ' __main__ ' in the Python module is similar to the C or Java language of the main function

Ps:python is committed to the readability of the program, __name__ conventions and difficult to read, perhaps one day Python will __name__ only as a module name, and in accordance with the conventional, set the module's main entrance

There are two rules in the UNIX programming Arts: 1 The design that conforms to most people's habit is OK; 2) The meaning of an object is definite, do not change to change;

Therefore, this design of Python is not OK

Python if __name__ = = ' __main__ ' What's hell is it?

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.