Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Saving functions is a general problem with Python (and Python is Sage's language). But Sage provides a partial solution in the modules sage.misc.fpickle. That module contains a function pickle_function, that translates a given function into a string (and then you can save the string). And it also contains a function unpickle_function, that translates that string into a function.


sage: from sage.misc.fpickle import pickle_function, unpickle_function
sage: def my_func(x): return x^2
sage: tmp = tmp_filename()
sage: save(pickle_function(my_func), tmp)
sage: recovered_function = unpickle_function(load(tmp))
sage: recovered_function(5)

pickle_function does have limitations (as explained in the docs), but perhaps it is enough for your application?