bob person personload class with import go through module name from person import person bob personload class with from use name directly we'll use from to load in our scriptjust because it' bit less to type to keep this simplecopy or retype in our new script the self-test lines from person py that make instances of our classesso we have something to store (this is simple demoso we won' worry about the test-code redundancy hereonce we have some instancesit' almost trivial to store them on shelve we simply import the shelve moduleopen new shelve with an external filenameassign the objects to keys in the shelveand close the shelve when we're done because we've made changesfile makedb pystore person objects on shelve database from person import personmanager load our classes bob person('bob smith're-create objects to be stored sue person('sue jones'job='dev'pay= tom manager('tom jones' import shelve db shelve open('persondb'for obj in (bobsuetom)db[obj nameobj db close(filename where objects are stored use object' name attr as key store object on shelve by key close after making changes notice how we assign objects to the shelve using their own names as keys this is just for conveniencein shelvethe key can be any stringincluding one we might create to be unique using tools such as process ids and timestamps (available in the os and time standard library modulesthe only rule is that the keys must be strings and should be uniquesince we can store just one object per keythough that object can be listdictionaryor other object containing many objects itself in factthe values we store under keys can be python objects of almost any sort--builtin types like stringslistsand dictionariesas well as user-defined class instancesand nested combinations of all of these and more for examplethe name and job attributes of our objects could be nested dictionaries and lists as in earlier incarnations in this book (though this would require bit of redesign to the current codethat' all there is to it--if this script has no output when runit means it probably workedwe're not printing anythingjust creating and storing objects in file-based database :\codemakedb py exploring shelves interactively at this pointthere are one or more real files in the current directory whose names all start with "persondbthe actual files created can vary per platformand just as in the built-in open functionthe filename in shelve open(is relative to the current working step (final)storing objects in database