class C(collections.defaultdict):

    def __init__(self):
        collections.defaultdict.__init__(self, list)

def __reduce__(self):
    t = collections.defaultdict.__reduce__(self)
return (t[0], ()) + t[2:]
c = C()
c[1].append(2)
c[2].append(3)
c2 = pickle.loads(pickle.dumps(c))
c2 == c
How to pickle and unpickle instances of a class that inherits from defaultdict?