469,920 Members | 2,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

how to exclude specific things when pickling?

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.
Sep 14 '08 #1
5 1566
On Sep 14, 10:53 am, "inhahe" <inh...@gmail.comwrote:
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).
Sep 14 '08 #2
inhahe 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.
Pickle uses the methods __getstate__, __setstate__ and __reduce__ /
__reduce_ex__ to access certain aspects of an object like e.g. the
state. You can find more information in the pickle docs.

Christian

Sep 14 '08 #3
Michael Palmer wrote:
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).
FYI:
__getstate__ is ignored when __reduce__ is available.

Christian

Sep 14 '08 #4
En Sun, 14 Sep 2008 12:06:44 -0300, Michael Palmer <m_********@yahoo.ca>
escribió:
On Sep 14, 10:53 am, "inhahe" <inh...@gmail.comwrote:
>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

Sep 16 '08 #5
On Sep 14, 9:53*am, "inhahe" <inh...@gmail.comwrote:
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.
Are you picturing a custom pickler object?
Sep 16 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by gong | last post: by
176 posts views Thread by Thomas Reichelt | last post: by
2 posts views Thread by Kirk Strauser | last post: by
3 posts views Thread by Gabe Matteson | last post: by
10 posts views Thread by Simon Brooke | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.