3

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

  • 3
    You'll want the `if __name__=="__main__"` if you want to import the file without the main function being invoked. – Dan D. Apr 27 '12 at 11:19
  • 1
    http://stackoverflow.com/questions/419163/what-does-if-name-main-do contains several good answers – Edvard Pedersen Apr 27 '12 at 11:20
  • Well, I understand now what it's for..... and I've tested it. It definitely prevents it from being run until called upon. However, why not just make code simpler and cleaner by not including the run option in this file and only run it in the file we're importing it into? Less key strokes? – BK_TheMadRussian Apr 27 '12 at 11:29
  • 1
    I think I'm actually understanding it a bit more now. We add that little line in there to be able to constantly modify the module and test it independently without having it run in the main file that we're importing it into, and without having to type and delete main() constantly to test it over and over after making changes. – BK_TheMadRussian Apr 27 '12 at 11:35
  • of course, you could omit the function call entirely. but then, it won't run when you run the module. the `__main__` test is just idiomatic for code you want to be run when running the module, but not when importing it. – ch3ka Apr 27 '12 at 11:35
  • Yeah, definitely starting to understand it more. It has a good use if you are developing something and constantly want to independently test modules. – BK_TheMadRussian Apr 27 '12 at 11:39

2 Answers2

2

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.

Denis
  • 7,127
  • 8
  • 37
  • 58
  • Oh, wait, I think I might understand now... so, it allows you to test a module on its own really quick to see what it does, while still have it available for loading into the main file without having it executed there unless called upon. I can see this being good if you don't want to repeatedly type and delete main() after making changes to it to see how it works. – BK_TheMadRussian Apr 27 '12 at 11:34
0

It's so that part isn't run when the file is imported by a different file. Only if the file is actually the first 'call' will the code be run.

Aeveus
  • 5,052
  • 3
  • 30
  • 42
  • Well, if the spontaneous running of the function in a different file is a concern. Why not just exclude that portion from the file and only run it in the new file. So you'd have from suchandsuch_file import main and then in the new file you can type main() to run it. – BK_TheMadRussian Apr 27 '12 at 11:21