Saturday, April 7, 2012

When to use '__main__' ?

When your script is run it as a command to the Python interpreter: python all of the code that is at indentation level 0 gets executed and functions and classes that are defined but none of their code gets ran. If will then read :
if __name__ == '__main__'
so it will execute the block standalone. In other words, when you use the __main__ this means the module is being run standalone
if __name__ == '__main__':
 print '... is being run by itself'
 print '... is being run directly'