The use of Python __name__ and __main__

Source: Internet
Author: User
Use of __name__ and __main__

#hello. py
Def SayHello ():
str= "Hello"
Print (str);

if __name__ = = "__main__":
Print (' This is main of module ' hello.py ')
SayHello ()

This calls itself to execute all, the following is the execution result, if __name__ = = "__main__" is as a test, call the top-level program code to do the internal unit test. __NAME__ is actually the file itself, __main__ as the main function entrance.

This is main of the module "hello.py"
Hello

If this hello.py is called by another. py file, only the SayHello () function will be executed, and the print syntax will not be executed. This time the __name__ is actually modul (function SayHello ()) itself, which is called Hello.sayhello ()
>>> Import Hello
>>> hello.__name__
' Hello '
>>>

The use of Python __name__ and __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.