Class Persistence using __dict__

Alex cut_me_out at
Mon Sep 4 23:19:50 CEST 2000

> But if you do it this way, what is the def of the class method that
> loads the pickle?

Well, I have a helper function that does the loading for me.  Something

class Test:

def load_Test(filename):
    return cPickle.load(filename)

If you're bent on doing it in the context of the class, you could do
something like this, I guess:

class Test:

    def __init__(self, **kw):

        if kw.has_key('pickle_filename'):

            # Load data in from a pickle.
            pickle = cPickle.load(kw['pickle_filename'])
            for attr in dir(pickle):
                # Transfer the attributes of the pickle to this instance
                setattr(self, attr, getattr(pickle, attr))

            # Do your usual initialization here.

t = Test(pickle_filename=fn)

...but it seems a little awkward to me.


The chain of destiny can only be grasped one link at a time.  
-- Sir Winston Churchill

More information about the Python-list mailing list