Created
August 7, 2014 00:26
-
-
Save RichardBarrell/941565a570729339240e to your computer and use it in GitHub Desktop.
__reduce__ gets used by pickle and cPickle, when defined by regular Python classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import pickle | |
>>> class C(object): | |
... def __init__(self, banana): | |
... self.banana = banana | |
... def __reduce__(self): | |
... return (C, (self.banana,)) | |
... | |
>>> pickle.dumps(C(1)) | |
'c__main__\nC\np0\n(I1\ntp1\nRp2\n.' | |
>>> pickle.loads(pickle.dumps(C(1))) | |
<__main__.C object at 0x7eff0efa9ed0> | |
>>> pickle.loads(pickle.dumps(C(1))).banana | |
1 | |
>>> import cPickle | |
>>> cPickle.dumps(C(1)) | |
'c__main__\nC\np1\n(I1\ntRp2\n.' | |
>>> cPickle.loads(cPickle.dumps(C(1))) | |
<__main__.C object at 0x7eff0efa9f90> | |
>>> cPickle.loads(cPickle.dumps(C(1))).banana | |
1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and to show that it definitely is getting called: