about functions question

Chris Mellon arkanes at gmail.com
Thu Oct 25 12:12:22 EDT 2007


On 10/25/07, Neil Cerutti <horpner at yahoo.com> wrote:
> On 2007-10-25, Bruno Desthuilliers
> <bruno.42.desthuilliers at wtf.websiteburo.oops.com> wrote:
> > The canonical case for small scripts is to have first all
> > functions and globals defined, then the main code protected by
> > a guard, ie:
>
> There's no reason to "protect" your main code in a small script.
>

There's also not much reason not to, and it prevents disaster or at
least unwanted side effects when you accidentally run pychecker or
pydoc over it.

Also, debugging scripts is a lot easier when you can import them into a shell.

> > if __name__ == '__main__':
> >    print SOME_CONST
> >    if not do_something():
> >      try_somethin_else()
>
> That idiom is useful in modules for launching tests or examples
> that should not be run when the module is imported.
>

I use it whenever there's any code I don't want run unless I'm
explicitly trying to do so.



More information about the Python-list mailing list