[Python-ideas] Just __main__

Ned Batchelder ned at nedbatchelder.com
Mon Jun 18 23:35:03 CEST 2012

On 6/18/2012 4:09 PM, Andrew McNabb wrote:
> On Mon, Jun 18, 2012 at 12:39:05PM -0700, Bruce Leban wrote:
>> The special value of __name__ and the proposed __main__() function are both
>> a bit magic. However, when I write if __name__ == '__main__' it's at least
>> clear that that if statement *will* be executed. It's just a question of
>> when the condition is true and if I don't know I can find out fairly
>> easily. (As I did the first time I saw it and probably other people on this
>> list did too.) On the other hand, it's not at all obvious that a function
>> named __main__ will be executed automagically.
> Given that C, Java, and numerous other languages automagically execute a
> function called "main", I would argue that a "__main__" function would
> actually be _less_ surprising than "if __name__ == '__main__'" for most
> new Python users.
But a __main__ function misses the whole point: that a module can be 
importable and runnable, and the if statement detects the difference.  
If you simply want a function that is always invoked as the main, then 
just invoke it:

    def main():
         blah blah


No need for special names at all.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120618/557754c0/attachment.html>

More information about the Python-ideas mailing list