The "standard" way is to do things inside the main function at the top of your file, and then call main() at the bottom. For instance.
def main(): print 'doing stuff' foo() bar() def foo(): print 'inside foo' def bar(): print 'inside bar' if __name__ == '__main__': main()
if if __name__ == '__main__': part ensures that main() will not be called if the file is imported into another python program, but only called when the file is launched directly.
Of course, "main" does not mean anything ... ( __main__ , however!) This is a psuedo symbol, but you could also call it do_stuff and then have if __name__ == '__main__': do_stuff() below.
Edit: You can also see Guido's advice when writing main . Also, Daenyth makes a great point (and beats me up to answer): The reason why you should do something like this is not "standard" or even allows it to define functions under your "main" code. The reason you should do this is because it encourages you to write modular and reusable code.
source share