Servando Garcia wrote: > > 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
When Hope.py is a module imported into another script, then __name__ equals the name of the module as you say. Like this: ### HopeModule.py print __name__ ### Hope.py import HopeModule That will print 'HopeModule' if you run python Hope.py But if the scrip itself is run, __name__ will equal '__main__': ### Hope.py print __name__ Now python Hope.py prints '__main__', contrary to what you said. Did you try it? If your installation does otherwise, it seems there is something wrong. > but why would I want to do this if __name__ == '__main__': From the above it follows that code following if __name__ == '__main__': gets executed when the script itself is run, but not when it is import edas a module by another script. > 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. Did you try that? The code should really be executed. -- If I have been able to see further, it was only because I stood on the shoulders of giants. -- Isaac Newton Roel Schroeven _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor