action description index file keys(index fetch the stored keys list (an iterable viewfound 'keyin file query see if there' an entry for key del file['key'delete remove the entry for key for key in fileiterate iterate over stored keys file close(close manual closenot always needed because shelves export dictionary-like interfacetoothis table is almost identical to the dbm operation table herethoughthe module name dbm is replaced by shelveopen calls do not require second argumentand stored values can be nearly arbitrary kinds of objectsnot just strings keys are still stringsthough (technicallykeys are always str which is encoded to and from bytes automatically per utf- )and you still should close shelves explicitly after making changes to be safeshelves use dbm internallyand some underlying dbms require closes to avoid data loss or damage recent changesthe shelve module now has an optional writeback argumentif passed trueall entries fetched are cached in memoryand written back to disk automatically at close time this obviates the need to manually reassign changed mutable entries to flush them to diskbut can perform poorly if many items are fetched--it may require large amount of memory for the cacheand it can make the close operation slow since all fetched entries must be written back to disk (python cannot tell which of the objects may have been changedbesides allowing values to be arbitrary objects instead of just stringsin python the shelve interface differs from the dbm interface in two subtler ways firstthe keys method returns an iterable view object (not physical listsecondthe values of keys are always str in your codenot bytes--on fetchesstoresdeletesand other contextsthe str keys you use are encoded to the bytes expected by dbm using the utf- unicode encoding this means that unlike dbmyou cannot use bytes for shelve keys in your code to employ arbitrary encodings shelve keys are also decoded from bytes to str per utf- whenever they are returned from the shelve api ( keys iterationstored values are always the bytes object produced by the pickler to represent serialized object we'll see these behaviors in action in the examples of this section storing built-in object types in shelves let' run an interactive session to experiment with shelve interfaces as mentionedshelves are essentially just persistent dictionaries of objectswhich you open and closec:\pp \dbasepython import shelve shelve files