Class Persistence using __dict__

Gene C gchiaramonte at yahoo.com
Mon Sep 4 19:55:51 EDT 2000


I think I'll just stick with pickling __dict__.

Thanks

> 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.






More information about the Python-list mailing list