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?
__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.
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.
“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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With