Search code examples
pythonfunctionboilerplate

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


Solution

  • 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.