Pickle failed __getstate__ on my customized class inherited dict

lampahome pahome.chen at mirlab.org
Tue Nov 19 21:23:04 EST 2019


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 ===

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.


More information about the Python-list mailing list