Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Google lesson - boilerplate to call a function

I was watching some videos on YouTube by Google and in the basic lessons they were showing how to create and call a function:

def main():
    print 'Hello'

if __name__ == '__main__':   # this is the boilerplate portion
    main()

Why would we need to take the extra step to create that boilerplate if we can just the code look like this:

def main():
    print 'Hello'

main()

The output will be exactly the same without the extra code.

-BK

like image 409
BK_TheMadRussian Avatar asked May 03 '26 06:05

BK_TheMadRussian


1 Answers

This is for test purpose. Then you write module you can test it in under name/main section and make sure what you code work properly. But when you call it from another module you test under this section will not call.

like image 60
Denis Avatar answered May 05 '26 20:05

Denis