Class Persistence using __dict__

Alex cut_me_out at hotmail.com
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
like 

class Test:
    pass

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))
        else:

            # Do your usual initialization here.
            pass

t = Test(pickle_filename=fn)

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

Alex.

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



More information about the Python-list mailing list