Use of __name__ in "python" python

Source: Internet
Author: User

py1.py

#! /usr/bin/env pythondef Test ():    print ' __name__ = ', __name__if __name__ = = ' __main__ ':    Test ()

py2.py

#! /usr/bin/env pythonimport py1.pydef Test ():    print ' __name__ = ', __name__   if __name__ = = ' __main__ ':    Test () C6/>print ' py1.py __name__ = ', py1.__name__

Execution Result:

__name__=__main__

py1.py __name__=py1
Through the results can be known, py2.py directly execute, then the value of the built-in variable __name__ is __main__, otherwise the name of the module, through this feature can add test code inside the IF statement, can improve the reduction of bugs, improve the robustness of the program.

if __name__ = = ' __main__ ':
Test ()

Use of __name__ in "python" python

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.