Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude object's field from pickling in python

Tags:

python

pickle

I would like to avoid pickling of certain fields in an instance of a class. Currently, before pickling I just set those fields to None, but I wonder whether there's more elegant solution?

like image 731
Art Avatar asked Feb 27 '10 01:02

Art


People also ask

What objects Cannot be pickled in Python?

Generally you can pickle any object if you can pickle every attribute of that object. Classes, functions, and methods cannot be pickled -- if you pickle an object, the object's class is not pickled, just a string that identifies what class it belongs to.

Can all Python objects be pickled?

Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk.

Why pickle is not good in Python?

Pickle on the other hand is slow, insecure, and can be only parsed in Python. The only real advantage to pickle is that it can serialize arbitrary Python objects, whereas both JSON and MessagePack have limits on the type of data they can write out.

What is the difference between pickling and Unpickling in Python?

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.


1 Answers

One way to handle instance attributes that are not picklable objects is to use the special methods available for modifying a class instance's state: __getstate__() and __setstate__(). Here is an example

class Foo(object):      def __init__(self, value, filename):         self.value = value         self.logfile = file(filename, 'w')      def __getstate__(self):         """Return state values to be pickled."""         f = self.logfile         return (self.value, f.name, f.tell())      def __setstate__(self, state):         """Restore state from the unpickled state values."""         self.value, name, position = state         f = file(name, 'w')         f.seek(position)         self.logfile = f 

When an instance of Foo is pickled, Python will pickle only the values returned to it when it calls the instance's __getstate__() method. Likewise, during unpickling, Python will supply the unpickled values as an argument to the instance's __setstate__() method. Inside the __setstate__() method we are able to recreate the file object based on the name and position information we pickled, and assign the file object to the instance's logfile attribute.

Reference: http://www.ibm.com/developerworks/library/l-pypers.html

like image 67
anijhaw Avatar answered Oct 03 '22 19:10

anijhaw