You'll have to use an actual function instead, one that is importable (not nested inside another function):
import cPickle as pickle
from numpy import sin, cos, array
def tmp(x):
return sin(x)+cos(x)
test = array([[tmp,tmp],[tmp,tmp]],dtype=object)
pickle.dump( test, open('test.lambda','w') )
The function object could still be produced by a lambda
expression, but only if you subsequently give the resulting function object the same name:
tmp = lambda x: sin(x)+cos(x)
tmp.__name__ = 'tmp'
test = array([[tmp, tmp], [tmp, tmp]], dtype=object)
because pickle
stores only the module and name for a function object; in the above example, tmp.__module__
and tmp.__name__
now point right back at the location where the same object can be found again when unpickling.
tmp(x)
function was defined. – Undertrump