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

Bruce Leban bruce at leapyear.org
Mon Nov 25 00:55:48 CET 2013


On Sun, Nov 24, 2013 at 2:27 PM, Greg Ewing <greg.ewing at canterbury.ac.nz>wrote:

> Can someone remind me why there's so much resistance to
> simply blessing a __main__() function? It would be
> straightforward and intuitive and in line with what
> just about every other language does.
>


What's not intuitive is that __main__ would be called sometimes and not
other times. Not knowing the idiom, seeing def __main__,  why would I
assume it's going to get executed automatically and that this is only true
of one of the files being executed?

The first time I saw __name__ == '__main__', I thought "Huh?" and went and
looked it up and learned how it worked. If I had seen def __main__, I
probably would have thought it worked just like C's main function which can
be in any file.

--- Bruce
I'm hiring: http://www.cadencemd.com/info/jobs
Latest blog post: Alice's Puzzle Page http://www.vroospeak.com
Learn how hackers think: http://j.mp/gruyere-security
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131124/21d10e6e/attachment-0001.html>


More information about the Python-ideas mailing list