I have to pickle an array of objects like this:
import cPickle as pickle from numpy import sin, cos, array tmp = lambda x: sin(x)+cos(x) test = array([[tmp,tmp],[tmp,tmp]],dtype=object) pickle.dump( test, open('test.lambda','w') )
and it gives the following error:
TypeError: can't pickle function objects
Is there a way around that?
Functions are pickled by name, not by code. Unpickling will only work if a function with the same name is present in in the same module (__main__ in your example) This is why pickling a lambda won't work: they have no individual names.
“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 dump replaces current file data.
To save a pickle, use pickle. dump . A convention is to name pickle files *. pickle , but you can name it whatever you want.
The built-in pickle module is unable to serialize several kinds of python objects (including lambda functions, nested functions, and functions defined at the command line).
The picloud package includes a more robust pickler, that can pickle lambda functions.
from pickle import dumps f = lambda x: x * 5 dumps(f) # error from cloud.serialization.cloudpickle import dumps dumps(f) # works
PiCloud-serialized objects can be de-serialized using the normal pickle/cPickle load
and loads
functions.
Dill also provides similar functionality
>>> import dill >>> f = lambda x: x * 5 >>> dill.dumps(f) '\x80\x02cdill.dill\n_create_function\nq\x00(cdill.dill\n_unmarshal\nq\x01Uec\x01\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00C\x00\x00\x00s\x08\x00\x00\x00|\x00\x00d\x01\x00\x14S(\x02\x00\x00\x00Ni\x05\x00\x00\x00(\x00\x00\x00\x00(\x01\x00\x00\x00t\x01\x00\x00\x00x(\x00\x00\x00\x00(\x00\x00\x00\x00s\x07\x00\x00\x00<stdin>t\x08\x00\x00\x00<lambda>\x01\x00\x00\x00s\x00\x00\x00\x00q\x02\x85q\x03Rq\x04c__builtin__\n__main__\nU\x08<lambda>q\x05NN}q\x06tq\x07Rq\x08.'
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