[Tutor] Explanation of Pickle

Eike Welk eike.welk at gmx.net
Tue Jan 30 17:34:31 CET 2007

Hello Vanam!

On Tuesday 30 January 2007 08:11, vanam wrote:
> can any one explain about pickle i read in the book but they have
> not provided any example for that so please explain with a simple
> example
A class can pickle itself like this:

class foo(object):
    #lots of code here

    def _loadPickle(self, fileName):
        '''Load data from a file in pickle format.'''
        f = open(fileName, 'rb')
        newStore = cPickle.load(f) 
        self.__dict__ = newStore.__dict__ #copy the data attributes

    def _savePickle(self, fileName):
        '''Dump the data into a binary pickle file'''
        f = open(fileName, 'wb')
        cPickle.dump(self, f, 2)

Kind regards,

More information about the Tutor mailing list