<meta content="text/html; charset=ISO-8859-1"
<body bgcolor="#FFFFFF" text="#000000">
On 6/18/2012 4:09 PM, Andrew McNabb wrote:
<pre wrap="">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:<br>
No need for special names at all.<br>