Modeling Systems

Overview


Your programs now have several ways to organize work. Functions give behavior a name. Dictionaries and lists give information a structure. Files let that information survive between runs.

As programs grow, another question becomes useful: how should a program organize the operations that belong to a particular thing?

Consider an equipment record:

item = {
"id": "E10",
"name": "Camera",
"category": "media",
"available": True
}

Several operations belong to this item. It can be borrowed if it is available. It can be returned if it is on loan. It can provide a record for a save file.

We could implement those operations as ordinary functions receiving the dictionary. That remains a valid design. A class offers another way to put state and the behavior that uses it together.

You have already worked with classes and objects — instances of classes. You have used built-in classes such as list, dict, and str. You have created objects by calling those classes, and you have called methods on those objects. This part shows how to define your own classes and methods.

After working through this part, you should be able to define classes whose methods keep their data valid, organize a small application into modules, and test its rules and file operations separately from its user interface.

The structure of this part is as follows:

Choose Where to Work

Continue using the Python and IDE setup from Part 5. Choose the environment according to the exercise:

ExercisesRecommended environment
All tracing, review, code-arranging, explanation, and feedback tasksBrowser
Programming exercises in Defining Classes and Creating Objects, Methods and Valid State, and Objects Working TogetherBrowser
Export an item and Export a libraryBrowser
Repair a note collection and Check a note collectionBrowser
Model moving objectsBrowser
Move a class into a module and Prepare a session moduleIDE
Restore library records and Keep a library fileIDE
AsteroidsBrowser for playing and model checks; IDE for checking saves across program restarts
Equipment libraryIDE

The IDE exercises practise working across source files and inspecting saved data. Download each into its own folder; follow the handout and Part 5’s setup chapter for run commands and submission instructions.

The Inline Editor is also available for these exercises. Browser-generated files are temporary, so check saving and loading within one execution there. Use your IDE to check that saved data survives closing and restarting a program.

Both programming exercises in Classes and Games have a Pyxel preview. Select main.py and choose Run to try the moving objects or play Asteroids. To run their sample checks, select check.py and choose Run as Python. Asteroids becomes playable after firing and collision handling work; saving and loading are later stages.

For each new idea, predict the result of a small example, run it, and change one input or operation. Then complete the nearby exercises.

After Objects Working Together, check that you can follow a call from a collection to one of its items. Identify which object changes, what the call returns, and which references reach the same object.

After Objects and Persistent Data, check that you can follow an object into a plain record and back into a new object. Explain why a rejected load leaves the current object unchanged.

Keep one working habit in mind:

Follow the method call. Identify what changes. Check the result.