[Tutor] User input question

alan.gauld@bt.com alan.gauld@bt.com
Thu, 8 Aug 2002 17:45:55 +0100


> Out of curiosity, the  if __name__ == "__main__":  line...
> Where does __name__ and __main__ refer to in the script?

Python has a bunch of "magic" names, usually surrounded by doubvle
underscores at each end.

__name__ is a value that python assgns to each file 
when it imports or runs it. If its imported __name__ 
is the name of the module ("sys","string" etc) but 
if the file is being run as a program python assigns 
the special value "__main__" to it.

So the if clause above only runs the code under it 
if the file is being used as a program script, but 
not if its imported. This is explained further in 
my tutor in the case study in the subsection 
'Turning it into a module'.

HTH,

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld