Is there any advantage to using a main() in python scripts?

Roy Smith roy at panix.com
Wed Dec 11 15:20:29 CET 2013


In article <mailman.3895.1386766655.18130.python-list at python.org>,
 marduk at letterboxes.org wrote:

> I would agree with the previous post but also add that I've stopped
> calling the main function "main()" and usually give it a more
> descriptive name, such as "bake_cookies()" or whatever.  I think that
> that makes it clearer what it's doing when used as a library and the 'if
> __name__ == '__main__'" already implies that it is the "main" script
> function.

If you're writing a library that's meant to be imported by other 
scripts, then that makes sense.  I tend to use main() for things that 
are done when your script is run as a stand-alone program.  That usually 
involves things like parsing command-line arguments, and configuring 
logging, neither of which you'd want to do in an importable library.



More information about the Python-list mailing list