[Tutor] unpickling a class

Andy W toodles@yifan.net
Sat, 22 Dec 2001 14:12:16 +0800


> OK, this make sense. I'm puzzled about the "... extract whatever you
> need from it" part. It doesn't seem as though the attributes that have
> been pickled are automatically unpickled.

Sorry I'm confusing at best. What I meant was, because as
self=pickle.load(...) doesn't work (as you found out), you'd need to
manually take the attributes from the unpickled instance.

The easiest way that I can think of...

> class Portfolio:
>     def __init__(self, file=None):
>         if file is not None:
>             if fileExists(file):
>                 f = open(file, 'r')

                  unpickled_instance = pickle.load(f)
                  self.__dict__.update(unpickled_instance.__dict__) #copies
attributes from unpickled_instance to self

Perhaps there's a better way though. Sorry for giving incomprehensible
answers!

HTH,
Andrew