[Python-ideas] Replacing the if __name__ == "__main__" idiom (was Re: making a module callable)

Antoine Pitrou solipsis at pitrou.net
Fri Nov 22 23:19:52 CET 2013


On Fri, 22 Nov 2013 14:02:27 -0800
David Mertz <mertz at gnosis.cx> wrote:

> I'm not in love with the *spelling* of " if __name__=='__main__': ", but I
> very frequently use the overall pattern.
> 
> Much--or even most--of the time when I write a module, I like to allow it
> to either do a minimal case of its basic functionality and/or have the
> module run some basic unit tests as a quick check against breakage.  So in
> contrast to Eric Snow, I try *to* make my files both scripts and modules.
>  I know this isn't the only possible approach, but I don't think it's bad
> or uncommon.

I agree with this, and we actually use it quite a bit in the stdlib
(try e.g. "python -m zipfile -h").

Regards

Antoine.




More information about the Python-ideas mailing list