dict subclass and pickle bug (?)

James Stroud jstroud at mbi.ucla.edu
Fri Dec 5 11:51:45 CET 2008

James Stroud wrote:
> Hello All,
> I subclassed dict and overrode __setitem__. When instances are 
> unpickled, the __setstate__ is not called before the keys are assigned 
> via __setitem__ in the unpickling protocol.
> I googled a bit and found that this a bug filed in 2003:
> http://bugs.python.org/issue826897
> It is still "open" with "normal" priority.

Here is the ugly "fix" I'm basically going to have to live with, it seems:

class DictPlus(dict):
   def __init__(self, *args, **kwargs):
     self.extra_thing = ExtraThingClass()
     dict.__init__(self, *args, **kwargs)
   def __setitem__(self, k, v):
       do_something_with(self.extra_thing, k, v)
     except AttributeError:
       self.extra_thing = ExtraThingClass()
       do_something_with(self.extra_thing, k, v)
     dict.__setitem__(self, k, v)
   def __setstate__(self, adict):

This violates this:

   Beautiful is better than ugly.

I can't imagine this bug has survived but I also can't imagine any 
better way to do this without specifying a different protocol, which 
would probably break other pickling I'm doing. I don't feel like finding 
out right now.

Maybe repeal pep 307 ;o)


James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095


More information about the Python-list mailing list