how to exclude specific things when pickling?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Sep 16 03:11:32 EDT 2008


En Sun, 14 Sep 2008 12:06:44 -0300, Michael Palmer <m_palmer45 at yahoo.ca>  
escribió:
> On Sep 14, 10:53 am, "inhahe" <inh... at gmail.com> wrote:

>> If I gather correctly pickling an object will pickle its entire  
>> hierarchy,
>> but what if there are certain types of objects anywhere within the  
>> hierarchy
>> that I don't want included in the serialization?  What do I do to  
>> exclude
>> them?   Thanks.
>
> If your class defines a  __getstate__ method, it is expected to return
> the pickled state of the entire class. You can for example del those
> items from self.__dict__ that you don't want pickled and then return
> dumps(self).

note: __getstate__ should return the *values* to be pickled, not the  
pickled state; else you end doing the work twice.
And "del those items from self.__dict__" isn't a good idea, in general;  
I'd use a *copy* of __dict__ instead.

-- 
Gabriel Genellina




More information about the Python-list mailing list