input and output by defaultprotocol is used this is the oldest pickle data format that stores objects in format understood by virtually all python versions howeverthis format is also incompatible with many of python' more modern features of user-defined classes such as slots protocol and use more efficient binary data representation to use these alternative protocolsyou would perform operations such as the followingimport pickle obj someobject( open(filename,'wb'pickle dump(obj, , pickle dump(obj, ,pickle highest_protocolf close(save using protocol use the most modern protocol it is not necessary to specify the protocol when restoring an object using load(the underlying protocol is encoded into the file itself similarlya shelve can be opened to save python objects using an alternative pickle protocol like thisimport shelve db shelve open(filename,protocol= it is not normally necessary for user-defined objects to do anything extra to work with pickle or shelve howeverthe special methods _getstate_ (and _setstate_ (can be used to assist the pickling process the _getstate_ (methodif definedwill be called to create value representing the state of an object the value returned by _getstate_ (should typically be stringtuplelistor dictionary the _setstate_ (method receives this value during unpickling and should restore the state of an object from it here is an example that shows how these methods could be used with an object involving an underlying network connection although the actual connection can' be pickledthe object saves enough information to reestablish it when it' unpickled laterimport socket class client(object)def _init_ (self,addr)self server_addr addr self sock socket socket(socket af_inet,socket sock_streamself sock connect(addrdef _getstate_ (self)return self server_addr def _setstate_ (self,value)self server_addr value self sock socket socket(socket af_inet,socket sock_streamself sock connect(self server_addrbecause the data format used by pickle is python-specificyou would not use this feature as means for exchanging data between applications written in different programming languages moreoverdue to security concernsprograms should not process pickled data from untrusted sources ( knowledgeable attacker can manipulate the pickle data format to execute arbitrary system commands during unpicklingthe pickle and shelve modules have many more customization features and advanced usage options for more detailsconsult "python runtime services lib fl ff