What is the best way to write a __getstate__
method that pickles almost all of an object's attributes, but excludes a few?
I have an object with many properties, including one that references an instancemethod. instancemethod's are not pickleable, so I'm getting an error when I try to pickle this object:
class Foo(object): def __init__(self): self.a = 'spam' self.b = 'eggs' self.c = 42 self.fn = self.my_func def my_func(self): print 'My hovercraft is full of eels' import pickle pickle.dumps(Foo()) # throws a "can't pickle instancemethod objects" TypeError
This __getstate__
method fixes this, but then I have to manually include all the properties I want to serialize:
def __getstate__(self): return { 'a': self.a, 'b': self.b, 'c': self.c }
That's not very scalable or maintainable if I have an object with many attributes or that changes frequently.
The only alternative I can think of is some kind of helper function that iterates through an object's properties and adds them (or not) to the dictionary, based on the type.
With pickle protocol v1, you cannot pickle open file objects, network connections, or database connections.
Pickle is unsafe because it constructs arbitrary Python objects by invoking arbitrary functions. However, this is also gives it the power to serialize almost any Python object, without any boilerplate or even white-/black-listing (in the common case).
Python Pickle dump dump() function to store the object data to the file. pickle. dump() function takes 3 arguments. The first argument is the object that you want to store. The second argument is the file object you get by opening the desired file in write-binary (wb) mode.
“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.
Pickle a Custom Class Although Pickle supports almost all the objects in Python, we still need to be careful when we pickle an object that was instantiated from a custom class. Briefly, the class needs to be existing when we load the pickled object back. For example, let’s define a simple class “Person” with two attributes and one method.
By default, pickle will retrieve the class and the attributes of an instance via introspection. When a class instance is unpickled, its __init__ () method is usually not invoked. The default behaviour first creates an uninitialized instance and then restores the saved attributes. The following code shows an implementation of this behaviour:
This exception inherits Exception. It is the base class for all other exceptions raised in pickling. This exception inherits PickleError. This exception is raised when an unpicklable object is encountered by Pickler. This exception inherits PickleError.
Introduction Literally, the term pickle means storing something in a saline solution. Only here, instead of vegetables its objects. Not everything in life can be seen as 0s and 1s (gosh! philosophy), but pickling helps us achieve that since it converts any kind of complex data to 0s and 1s (byte streams).
The only alternative I can think of is some kind of helper function that iterates through an object's properties and adds them (or not) to the dictionary, based on the type.
Yeah, I think that's pretty much what you're left with, if you want enough "magic" to allow yourself to be lazy (and/or allow for dynamically added attributes). Keep in mind that "pickle
can't handle this" isn't the only reason you might not want to include something in the pickled state.
But it's not as hard as you seem to think, assuming you have code for the "should I pickle this?" logic:
def __getstate__(self): return {k:v for (k, v) in self.__dict__.items() if should_pickle(v)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With