After reading pickle documentation, I got an impression that a class needs to implement either __reduce__ or __getstate__ to get pickled correctly. But how pickling of dictionaries work then? They don’t have any of those attributes:
> dict(a=1).__reduce__()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-30-bc1cbd43305b> in <module>()
----> 1 dict(a=1).__reduce__()
/usr/lib/python2.6/copy_reg.pyc in _reduce_ex(self, proto)
68 else:
69 if base is self.__class__:
---> 70 raise TypeError, "can't pickle %s objects" % base.__name__
71 state = base(self)
72 args = (self.__class__, base, state)
TypeError: can't pickle dict objects
> dict(a=1).__getstate__()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-31-00932fb40067> in <module>()
----> 1 dict(a=1).__getstate__()
AttributeError: 'dict' object has no attribute '__getstate__'
Also, how classes derived from dict are pickled?
The pickle module handles a number of types “natively”. The types it doesn’t handle natively will need to implement the “pickle protocol”. Dicts, and simple subclasses, are handled natively.