Question regarding style/design..

Michael Torrie torriem at gmail.com
Sat Jul 18 00:55:26 EDT 2009


Wells Oliver wrote:
> Sometimes I see relatively small application, generally task scripts,
> written as essentially a list of statements. Other times, I see them neatly
> divided into functions and then the "if __name__ == '__main__':" convention.
> Is there a preference? Is there an... application scope such that the
> preference shifts from the former to the latter? I understand the use of the
> __name__ == 'main' convention for building unit tests, but I'm mixed on
> using it in scripts/small applications.
> Thanks for any thoughts!

I always use the name guard thing for the simple reason that it lets me
reuse code by importing it as a module into other things.  My programs
often serve double purposes.  One is to be a standalone utility.  The
other is to be able to be used as a library from other code.  So the
name guard is essential.  For code that is intended to be in modules,
the name guard thing is indispensable for running unit tests.

For very short, one-off things, I may leave it off.  In any case I much
prefer having a name guard, then running various functions than the Java
style of writing a stupid static class with a static main method.
Always seemed like a paradigm hack to me.



More information about the Python-list mailing list