1 | initial version |
I think the answer crucially depends on what you mean by the word "function".
It could be a Python or Cython function, either defined in an interactive session or in a module. It could be a symbolic expression, or perhaps could be a polynomial, which are sometimes mistaken for a function.
Symbolic function is easy to pickle:
sage: f(x) = x^2
sage: type(f)
<type 'sage.symbolic.expression.Expression'>
sage: tmp = tmp_filename()
sage: save(f,tmp)
sage: load(tmp)
x^2
Similarly, a polynomial is easy to pickle:
sage: P.<x> = QQ[]
sage: p = x^2
sage: type(p)
<type 'sage.rings.polynomial.polynomial_rational_flint.Polynomial_rational_flint'>
sage: save(p,tmp)
sage: load(tmp)
x^2
However, if you are really talking about a Python function (e.g., a lambda function), then there is a general problem: You can't easily pickle Python functions defined in an interactive session. And Python is Sage's programming language.
sage: def f(x):
....: return x
....:
sage: save(f,tmp)
---------------------------------------------------------------------------
PicklingError Traceback (most recent call last)
/home/simon/SAGE/sage-5.0.beta7/devel/sage-main/<ipython console> in <module>()
/home/simon/SAGE/sage-5.0.beta7/local/lib/python2.7/site-packages/sage/structure/sage_object.so in sage.structure.sage_object.save (sage/structure/sage_object.c:8647)()
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
However, if you define the same function in some Python module, then pickling would work.
And what do you mean by the "contents" of a function?
Do you mean, you have a Python function and want to see its code?
Then again, if it is defined in a module, then it works easily: If you have any object X
(function or anything else) in Sage, then you can try to see its code by appending two question marks and hitting return (or shift-return in the Sage notebook). And with edit(X,'vim')
(inserting another editor name if you don't like vim), then you could actually edit the code.
But if the function is only defined in an interactive session, then Python can not find the code.