why a main() function?

Calvin Spealman ironfroggy at gmail.com
Mon Sep 18 22:08:31 CEST 2006


On 18 Sep 2006 12:40:00 -0700, beliavsky at aol.com <beliavsky at aol.com> wrote:
> I think I read a suggestion somewhere to wrap the code where a Python
> script starts in a main() function, so one has
>
> def main():
>     print "hi"
>
> main()
>
> instead of
>
> print "hi"
>
> What are the advantages of doing this?

It is useful both importating scripts without running them, for
debugging or reusing parts of them later, and also for adding small
test scripts to modules to allow them to be run for testing purposes.



More information about the Python-list mailing list