This question already has an answer here:
- Pickle all attributes except one 6 answers
I am using gnosis.xml.pickle to convert an object of my own class to xml. The object is initialized so that:
self.logger = MyLogger()
But when I do dump the object to a string I get an exception stating that the pickler encountered an unpickleable type (thread.lock).
Is there a way to 'tag' the logger attribute so that pickler will know not to try and pickle that attribute?
Your class can implement the special method
__getstate__
to return exactly what parts of an instance it wants to be pickled.There are several possible variants on that (though
__getstate__
and its optional companion method__setstate__
are most general) -- see the online Python doc page forpickle
, to which I already pointed above because it's the one documenting__getstate__
.You can define two methods,
__getstate__
and__setstate__
, to your class to override the default pickling behavior.http://docs.python.org/library/pickle.html#object.__getstate__
__getstate__
should return a dict of attributes that you want to pickle.__setstate__
should setup your object with the provided dict.Note that
__init__
won't be called when unpickling so you'll have to create your logger in__setstate__
This might be a better solution since it will allow an object created via
copy.deepcopy
to still have aself.logger
: