[Python-ideas] Just __main__

Ned Batchelder ned at nedbatchelder.com
Tue Jun 19 02:37:56 CEST 2012


On 6/18/2012 6:39 PM, Matt Chaput wrote:
>> 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
>>
>>     main()
>>
>> No need for special names at all.
>
> I'm afraid you're the one who's missed the point... the interpreter 
> would only call __main__() if __name__ == "__main__"
>
> Some people will cry "magic", but to me this is about what makes sense 
> when you explain it to someone, and I think __main__() makes more 
> sense (especially to someone with experience in other languages) than 
> "if __name__ == "__main__""
>
I understand the proposal now, and yes, it is "magic".  Explicit is 
better than implicit.  I like this explanation: "When you run a Python 
program, all the statements are run, from top to bottom." better than, 
"When you run a Python program, all the statements are run, from top to 
bottom, and then if there is a __main__ function (which there need not 
be), then it is invoked."

Python is full of constructs that are simpler than other languages, 
which when used in conventional ways, act similar to other languages.  
No need to complicate things to make it easier for C programmers to 
understand.  There's a lot they need to get used to in Python, and "if 
__name__ == '__main__':" is not difficult.

--Ned.

> Matt
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list