between objects when loaded an alternative approach is to use pickler and unpickler objects pickler(file [protocol ]creates pickling object that writes data to the file object file with the specified pickle protocol an instance of pickler has method dump(xthat dumps an object to file once has been dumpedits identity is remembered if subsequent dump(operation is used to write the same objecta reference to the previously dumped object is saved instead of writing new copy the method clear_memo(clears the internal dictionary used to track previously dumped objects you would use this if you wanted to write fresh copy of previously dumped object (that isif its value changed since the last dump(operationunpickler(filecreates an unpickling object that reads data from the file object file an instance of unpickler has method load(that loads and returns new object from file an unpickler keeps track of objects it has returned because the input source might contain an object reference created by the pickler object in this caseu load(returns reference to the previously loaded object the pickle module works with most kinds of normal python objects this includesn none numbers and strings tupleslistsand dictionaries containing only pickleable objects instances of user-defined classes defined at the top level of module when instances of user-defined class are pickledthe instance data is the only part that gets pickled the corresponding class definition is not saved--insteadthe pickled data merely contains the name of the associated class and module when instances are unpickledthe module in which the class is defined is automatically imported in order to access the class definition when re-creating instances it should also be noted that when restoring an instancethe _init_ (method of class is not invoked insteadthe instance is re-created through other means and the instance data restored one restriction on instances is that the corresponding class definition must appear at the top level of module (that isno nested classesin additionif the instance' class definition was originally defined in _main_ _that class definition must be manually reloaded prior to unpickling saved object (because there' no way for the interpreter to know how to automatically load the necessary class definitions back into _main_ when unpicklingit is not normally necessary to do anything to make user-defined class work with pickle howevera class can define customized methods for saving and restoring its state by implementing the special methods _getstate_ (and _setstate_ (the _getstate_ (method must return pickleable object (such as string or tuplerepresenting the state of the object the _setstate_ (method accepts the pickled object and restores its state if these methods are undefinedthe default behavior is to pickle an instance' underlying _dict_ attribute it should be noted that if these methods are definedthey will also be used by the copy module to implement the shallow and deep copy operations lib fl ff