[Tutor] main

bob bgailer at alum.rpi.edu
Sun Nov 6 06:26:08 CET 2005


At 03:39 PM 11/5/2005, Shi Mu wrote:
>It is very hard for me to understand why we need the following line?
>if __name__ == "__main__":

We don't need it. Often we code a module for importing into another module. 
But sometimes we also want to run the module independently as a Python 
program, perhaps just to test it, or for other purposes.
if __name__ == "__main__": is one way to test whether the module has been 
imported or is running standalone. When run standalone __name__ is 
"__main__". When imported __name__ is the module name.

Now is it easier to understand?



More information about the Tutor mailing list