[Python-Dev] Strange memo behavior from cPickle

Bruce Christensen t-bruch at microsoft.com
Tue Aug 1 23:52:42 CEST 2006


We seem to have stumbled upon some strange behavior in cPickle's memo
use when pickling instances.

Here's the repro:

[mymodule.py]
class C:
    def __getstate__(self): return ('s1', 's2', 's3')

[interactive interpreter]
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cPickle
>>> import mymodule
>>> class C:
...     def __getstate__(self): return ('s1', 's2', 's3')
...
>>> for x in mymodule.C(), C(): cPickle.dumps(x)
...
"(imymodule\nC\np1\n(S's1'\nS's2'\np2\nS's3'\np3\ntp4\nb."
"(i__main__\nC\np1\n(S's1'\nS's2'\nS's3'\ntp2\nb."
>>>

Note that the second and third strings in the instance's state are
memoized in the first case, but not in the second. Any idea why this
occurs (and why the first element is never memoized)?

--Bruce


More information about the Python-Dev mailing list