Python if __name__ = = ' __main__ '

Source: Internet
Author: User


Profile:

I've been writing about the shell recently in learning Python, and in learning to see the example code in the book has this line:

if __name__ = = ' __main__ '

Baidu read a lot of explanations did not thoroughly understand, and continue to study the final summary, if there is a wrong place please root is greatly appreciated.



Many of the explanations on the web mean that the code is meant to be called. That's right, but not all, for a better understanding I wrote two scripts to test (here I write on the virtual machine directly when learning, or the use of Python IDE to improve our efficiency)

Script 1:

[[email protected] tmp]# cat name.py#!/usr/bin/env python def ppy (): print "Python" def BBY (): print "atiger77" if __name_ _ = = ' __main__ ': Ppy () BBY () [[email protected] tmp]#

Note: a function called Ppy and BBY is written in name.py, and the function of the two functions is to output "Python and atiger77" and invoke them at the end ().

Run Script 1:

[email protected] tmp]# python name.pypythonatiger77

After running the discovery saw the Ppy function content description conforms to the IF condition, here to explain is when executes the script itself is certainly conforms to if __name__ = = ' __main__ ': the situation, since all is true why still want to write this judgment, we continue to look down.


Script 2:

[[email protected] tmp]# cat tname.pyfrom name Import *ppy () BBY ()

Note: There are only 3 lines of content in script 2, the first line invokes the name.py first script with the Import command, and then uses the Ppy and BBY functions in the first script.

Script 2 Execution results:

[email protected] tmp]# python tname.pypythonatiger77

This shows that the output echo of the ppy and BBY functions is successful, and the contents of the name.py function are then modified.


Let's comment on the name.py if __name__ = = ' __main__ ': statement, test again:

[[email protected] tmp]# cat Name.pydef ppy (): print "Python" def BBY (): print "atiger77" #if __name__ = = ' __main__ ':p py () BBY () [[email protected] tmp]# python name.pypythonatiger77[[email protected] tmp]#

Note: It should be noted here that Python tightly controls indentation and will not output content until the Ppy and BBY are commented out before the IF statement.


Let's do tname.py again, we can see the following echo:

[email protected] tmp]# python tname.pypythonatiger77pythonatiger77

It can be seen that without the If judgment call will be name.py all the content is executed once, so there will be output 2 times the contents of the BBY and ppy functions.


Summarize:

Here again to look at the summary of Baidu will be very clear, when the program is running itself, its __name__ value is the string "__main__", if it is called by other programs, then its __name__ value is not the string "__main__", The function of using the following judgment is to make the program execute only if it is running, and if it is only called, then it will not run.


Because just learn python soon wrote the wrong place also please point out, I am engaged in Linux operation and maintenance work if have like-minded friends we can learn to communicate together.

Number: Atiger77

This article is from "a pig like Linux" blog, make sure to keep this source http://atiger77.blog.51cto.com/5258236/1675353

Python if __name__ = = ' __main__ '

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.