On Sat, Feb 11, 2012 at 10:46 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote: > The problem is that defaultdict defines a custom __reduce__ method > which is used by the pickle protocol to determine how the object > should be reconstructed. It uses this to reconstruct the defaultdict > with the same default factory, by calling the type with a single > argument of the default factory. Since your subclass's __init__ > method takes no arguments, this results in the error you see. > > There are a couple of ways you could fix this. The first would be to > change the signature of the __init__ method to take an optional > argument accepting the default factory instead of hard-coding it, like > this: > > def __init__(self, default_factory=int): > super().__init__(default_factory, Joe=0) > > The other would be to just fix the __reduce__ method to not pass the > default factory to your initializer, since it is hard-coded. That > would look like this: > > def __reduce__(self): > return (type(self), ())
It occurs to me that there's also an option 3: you don't really need a defaultdict to do what you're trying to do here. You just need a dict with a custom __missing__ method. That could look something like this: class Dog(dict): def __missing__(self): return 0 And then you don't have to worry about the weird pickle behavior of defaultdict at all. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list