[Tutor] setstate trouble when unpickling

Jeff Peery jeffpeery at yahoo.com
Fri Jan 25 07:41:00 CET 2008


Hello,
   
  I've got a fairly simple class instance that I'm pickling. I'm using setstate to update the instance when I unpickle. Although the setstate doesn't seem to be called upon unpickling... here's an example, if anyone see's what I'm doing wrong, please let me know. Thanks!
   
  so I say start out with this class:
   
  class dog:
      def __init__(self):
          self.num_legs = 4
   
  then I pickle the instance of:
  sparky = dog()
   
  then I update my dog class to:
  class dog:
      def __init__(self):
          self.hair_color = 'brown'
          self.num_legs = 4
   
      def __setstate__(self, d):
          if 'hair_color' not in d:
              d['hair_color'] = 'brown'
          self.__dict__.update(d)
          print 'did this work?'
   
  Now when I unpickle the original pickled object sparky I would hope to see 'did this work' and I would also hope that sparky would have updated to have the attribute 'hair_color' but nothing of the sort happens. if just unpickles sparky without updating the attributes as I was hoping.
   
  Thanks!
   
  Jeff



       
---------------------------------
Never miss a thing.   Make Yahoo your homepage.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080124/dc7580bd/attachment.htm 


More information about the Tutor mailing list