[Tutor] main()

Servando Garcia servando at mac.com
Sat May 21 15:37:59 CEST 2005



  if __name__ == '__main__':

  what is the meaning and importance of this code line.  I have been 
able to glean some information. When you call a script __name__ is set 
to the "Name" of the script called.  example:   python Hope.py     
__name__ = Hope

but why would I want to do this   if __name__ == '__main__':

here a code snippet that I am trying to work through

#
#       MAIN
#

if __name__ == '__main__':
         import sys
         filename = sys.argv[1]
         f = open(filename)
         generate(semantic(parse(scan(f))))
         f.close()

I feel that if I am calling the script Hope.py than the above code 
should never get to run because __name__ is equal to "Hope" so why even 
write it.



More information about the Tutor mailing list