if __name__ == '__main__': main()
It may seem pointless to have a
main() function, since you could just put that code in the global scope at the bottom of the program instead, and the code would run the exact same. However, there are two good reasons to put them inside of a
First, this lets you have local variables whereas otherwise the local variables in the
main() function would have to become global variables. Limiting the number of global variables is a good way to keep the code simple and easier to debug. (See the "Why Global Variables are Evil" section in this chapter.)
Second, this also lets you import the program so that you can call and test individual functions. If the memorypuzzle.py file is in the C:\Python32 folder, then you can import it from the interactive shell. Type the following to test out the
getBoxAtPixel() functions to make sure they return the correct return values:
>>> import memorypuzzle >>> memorypuzzle.splitIntoGroupsOf(3, [0,1,2,3,4,5,6,7,8,9]) [[0, 1, 2], [3, 4, 5], [6, 7, 8], ] >>> memorypuzzle.getBoxAtPixel(0, 0) (None, None) >>> memorypuzzle.getBoxAtPixel(150, 150) (1, 1)
When a module is imported, all of the code in it is run. If we didn’t have the
main() function, and had its code in the global scope, then the game would have automatically started as soon as we imported it, which really wouldn’t let us call individual functions in it.
That’s why the code is in a separate function that we have named
main(). Then we check the built-in Python variable
__name__ to see if we should call the
main() function or not. This variable is automatically set by the Python interpreter to the string
'__main__' if the program itself is being run and
'memorypuzzle' if it is being imported. This is why the
main() function is not run when we executed the
import memorypuzzle statement in the interactive shell.
This is a handy technique for being able to import the program you are working on from the interactive shell and make sure individual functions are returning the correct values by testing them one call at a time.