Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__getstate__ method not being called when pickling a subclass of set

Tags:

python

pickle

From what I understand, when calling pickle.dumps on an object, it will call the object's __getstate__ method (if it has one) to determine what to pickle.

If I create a class such as:

class DictClass(dict):
    def __getstate__(self):
        print "pickling"
        return self

I get this result:

>>> pickle.dumps(DictClass())
pickling
'ccopy_reg\n_reconstructor\np0...'

I can do the same thing, replacing 'dict' with 'list':

class ListClass(list):
    def __getstate__(self):
        print "pickling"
        return self

>>> pickle.dumps(ListClass())
pickling
'ccopy_reg\n_reconstructor\np0...'

But if I use 'set', something different happens:

class SetClass(set):
    def __getstate__(self):
        print "pickling"
        return self

>>> pickle.dumps(SetClass())
'c__main__\nSetClass...'

The __getstate__ method doesn't get called. Why is this, and is it possible to specify what part of a subclass of a set to pickle?

like image 308
ackien Avatar asked Sep 20 '14 15:09

ackien


People also ask

What is __ Getstate __ Python?

__getstate__ should return object (representing class state) which will be pickled and saved. __setstate__ should take object from parameter and use it to retrieve class state as it was before.

What is __ reduce __ in Python?

Whenever an object is pickled, the __reduce__ method defined by it gets called. This method returns either a string, which may represent the name of a Python global, or a tuple describing how to reconstruct this object when unpickling.

What is pickling and Unpickling in Python with example?

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.

How do you pickle in Python?

First, import pickle to use it, then we define an example dictionary, which is a Python object. Next, we open a file (note that we open to write bytes in Python 3+), then we use pickle. dump() to put the dict into opened file, then close. Use pickle.


1 Answers

list does not implement __reduce__(), whereas set does:

>>> list().__reduce__()
...
TypeError: can't pickle list objects
>>> set().__reduce__()
(<type 'set'>, ([],), None)

It's the last tuple in the above example that gets pickled, so SetClass.__getstate__() never enters the picture.

like image 154
NPE Avatar answered Oct 30 '22 10:10

NPE