Chapter 7. Organizing Programs
In Chapter 6, you began using Python's features to create separate classes that can be used to create entirely self-contained objects. Classes and the objects that are created from them are tools that enable you to gather data and functions into a contained space so Figure that they can be viewed as part of a larger entity.
So far, the definitions of classes have all been in a single file and were not run in the way you normally think of programs being run. Instead, they were invoked interactively so that you could use them as you would from within another program. However, if you wanted to use the classes you've written with what you know so far, you would make the same file that defined the classes the program. That means putting all of the classes at the beginning of the file, and the important decision-making code at the end. The end is where it takes the most time to find the code that you're going to want to find the most often.
Another cautionary note needs to be sounded. Classes are very useful, but not all problems should be solved by creating a class. Sometimes the work of designing them is overkill, and other times what you really need are functions that don't require the long life span that data and methods can have in objects.
To make Python more useful, therefore, it offers you the great feature of enabling you to create modules that create a named scope for functions and data, but which are simpler than classes and objects. Modules give ...
Get Beginning Python®: Using Python 2.6 and Python 3.1 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.