views:

201

answers:

5
+13  A: 

Without the main sentinel, the code would be executed even if the script was imported as a module.

Ignacio Vazquez-Abrams
Also worth noting that having a `main()` function makes it possible to run that code with: `import module; module.main()`. If the code were just in the `if` block, it couldn't be run from elsewhere.
FogleBird
+2  A: 

Consider the second script. If you import it in another one, the instructions, as at "global level", will be executed.

Noe
+7  A: 

if the content of foo.py

print __name__
if __name__ == '__main__':
    print 'XXXX'

A file foo.py can be used in two ways.

imported in another file : import foo

In this case __name__ is foo, the code section does not get executed and does not print 'XXXX'.

executed directly : python foo.py

When it is executed directly, __name__ is same as __main__ and the code in that section is executed and prints 'XXXX'

One of the use of this functionality to write various kind of unit tests within the same module.

pyfunc
+3  A: 

"What does if __name__==“__main__”: do?" has already been answered.

Having a main() function allows you to call its functionality if you import the module. The main (no pun intended) benefit of this (IMHO) is that you can unit test it.

Johnsyweb
+1  A: 

Everyone else already answered it, but I think I still have something else to add.

Reasons to have that if statement calling main() (in no particular order):

  • Other languages (like C and Java) have a main() function that is called when the program is executed. Using this if, we can make Python behave like them, which feels more familiar for many people.

  • Code will be cleaner, easier to read, and better organized. (yeah, I know this is subjective)

  • It will be possible to import that python code as a module without nasty side-effects.

    • This means it will be possible to run tests against that code.

    • This means we can import that code into an interactive python shell and test/debug/run it.

But, you are not required to write a main() function and call it inside an if statement.

I myself usually start writing small throwaway scripts without any kind of function. If the script grows big enough, or if I feel putting all that code inside a function will benefit me, then I refactor the code and do it. This also happens when I write bash scripts.

Even if you put code inside the main function, you are not required to write it exactly as that. A neat variation could be:

import sys

def main(argv):
    # My code here
    pass

if __name__ == "__main__":
    main(sys.argv)

This means you can call main() from other scripts (or interactive shell) passing custom parameters. This might be useful in unit tests, or when batch-processing. But remember that the code above will require parsing of argv, thus maybe it would be better to use a different call that pass parameters already parsed.

In a object-oriented application I've written, the code looked like this:

class MyApplication(something):
    # My code here

if __name__ == "__main__":
    app = MyApplication()
    app.run()

So, feel free to write the code that better suits you. :)

Denilson Sá