[Tutor] using -i flag with '''if __name__ == "__main__":'''

Kent Johnson kent37 at tds.net
Wed May 18 05:11:21 CEST 2005


Terry Carroll wrote:
> I've often found it convenient to run a Python program I'm developing with 
> the -i flag. I find it convenient to use as a post-mortem as it hits bugs, 
> or to explore data structures.
> 
> I've recently started using the construct 
> 
>   if __name__ == "__main__":
>      main()

I think this approach to debugging won't scale well and you are just seeing the tip of the iceberg. 
As your programs get more complex you will encapsulate more and more functionality into functions 
and classes and the variables they use will not in general be visible from the global scope.

I often find that the information in the traceback and exception are enough to figure out the 
problem; if not, a few print statements can help. You will get better at this with experience.
An alternative is to use a debugger such as pdb or the one built into IDLE.

This recipe looks interesting - it dumps the values of the variables at the time of an uncaught 
exception. I haven't tried it but it looks like it could be useful when combined with the excepthook 
recipe John cited:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52215

Kent



More information about the Tutor mailing list