Usage of main()

Mel mwilson at
Fri Sep 4 13:37:58 CEST 2009

Manuel Graune wrote:
[ ... ]
> thanks for your answer. What you are explaining is exactly why I tried
> it in the first place. I'm just wondering why (this is my impression,
> not necessaryly the reallity) none of the recommended texts on python
> put this in the first chapters. Instead - if it is mentioned at all -
> it is hidden somewhere in the "advanced" sections. Even if the reason
> for this is (I'm guessing...) because it is thought to be to complicated
> to explain the "why" right at the beginning, it probably would not hurt
> to just tell that this is the "correct" way of doing things right at the
> start and add a footnote.

Maybe it's the "correct" way, but it isn't *the* correct way.  In my 
experience, when I import a program, it isn't because I want to run it 
straight through.  For that there's `exec`, and subprocess and what not.  
More likely I want to get at the internals -- maybe produce my own output 
from the intermediate results, for example.  For that, a monolithic `main` 
function is beside the point.

For a sizeable program you can get the same speed advantage, to within five 
9s or so, with a structure like

## ...
if __name__ == '__main__':


More information about the Python-list mailing list