On Thu, October 25, 2007 4:41 pm, RR4CLB wrote: > Take a look at this: > Python Library Reference ... > __main__ > -- Top-level script environment > This module represents the (otherwise anonymous) scope in which the > interpreter's main program executes -- commands read either from standard > input, from a script file, or from an interactive prompt. It is this > environment in which the idiomatic ``conditional script'' stanza causes a > script to run: if __name__ == "__main__": main() Previous Page
Ah, OK, I think I see the confusion's source. When Python runs, the file that you're actually running is recognized by the interpreter as having a special designation, "__main__". __This syntax__ is also used unofficially for lines like: __version__ = "12.7.41" Having the line: if __name__ == "__main__": ...At the bottom of your code creates a conditional auto-run section, something that runs whenever the Python file is loaded, but only if it's being run as the main program as opposed to being imported by another file. For instance, I've got a module called Coral that makes a tiled graphics engine but doesn't include game logic. To test Coral I've got some code at the bottom of that module's code inside that "if" line, set up to run a demo/test. So when I open up Coral.py and run it, the demo runs, but when I say "import Coral" in something else, the demo doesn't run. The line "if __name__ == "__main__": main()" is a slightly different way of doing what I did, just putting the "demo" code inside a function instead of sitting at the bottom of the file. There isn't actually a built-in function called "main". For Pygame purposes, you can and should make your own main loop, and don't need to trap your code inside somebody else's loop logic.
