Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How dict objects are pickled?

Tags:

python

pickle

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?

like image 994
user1084871 Avatar asked Apr 28 '12 15:04

user1084871


People also ask

Can a dictionary be pickled?

In general, pickling a dict will fail unless you have only simple objects in it, like strings and integers. Even a really simple dict will often fail. It just depends on the contents.

How do you pickle an object in Python?

Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk. What pickle does is that it “serializes” the object first before writing it to file. Pickling is a way to convert a python object (list, dict, etc.)

Can pickles save objects?

The pickle module can store things such as data types such as booleans, strings, and byte arrays, lists, dictionaries, functions, and more. Note: The concept of pickling is also known as serialization, marshaling, and flattening. However, the point is always the same—to save an object to a file for later retrieval.

How do you pickle a model?

To save the ML model using Pickle all we need to do is pass the model object into the dump() function of Pickle. This will serialize the object and convert it into a “byte stream” that we can save as a file called model. pkl .


2 Answers

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.

like image 63
thebjorn Avatar answered Oct 04 '22 23:10

thebjorn


The __reduce__ and __getstate__ methods are meant to be the lower bound of pickling methods, for you to implement on your custom classes when it needs some special treatment from the interpreter.

For example, if an instance of an extension class is inside a dictionary you are trying to pickle, that renders the whole dictionary unpickable if your class doesn't implement those methods saying how to pickle it.

The interpreter knows how to pickle builtins, and to pickle a dictionary you should use pickle.dump or pickle.dumps method, not by calling __reduce__ or __getstate__.

like image 28
Pedro Werneck Avatar answered Oct 05 '22 01:10

Pedro Werneck