Pickle failed __getstate__ on my customized class inherited dict
dieter
dieter at handshake.de
Wed Nov 20 01:58:06 EST 2019
lampahome <pahome.chen at mirlab.org> writes:
> I make a class Wrapper inherited from dict and met problem when I want to
> pickle it.
>
> Is there anyway to make __getstate__ of Wrapper to output a normal
> dict?(Output a dict will help pickleing easily)
>
>
> === code ===
> import pickle
> class Wrapper(dict):
> def __getattr__(self, attr):
> return self[attr]
> def __getstate__(self):
> # blablabla
>
> d=Wrapper(zip((1,2), (3,4)))
> pickle.dumps(d)
> === code ===
You have forgotten to tell us which problem the code above revealed.
> When I tried overwrite __getstate__ to below:
> def __getstate__(self):
> return self.__repr__()
> pickle it will shows:
> b'\x80...__main__...Wrapper...' <- I don't know why it shows the class name.
> That makes me confused.
It should not: pickling a class instance entails pickling a class
reference and the instance's state. The class name is part
of the class reference.
More information about the Python-list
mailing list