When using ipython I often want to save specific functions I have defined during my session, e.g.:
In [1]: def func1():
...: pass
...:
In [2]: %save func1.py func1
func1 is neither a string nor a macro.
Instead I have to pick out the function definition line number from enumerate(_ih), or manually copy and paste from vim if I have %edit‘d the function.
Is there a way to achieve %save func1.py func1? I feel like it should be possible as ipython has access to the definition when using %edit.
Edit
Line based saving doesn’t work if I have at some point edited the function with %ed. I’m looking for a way to save the new function definition in this case.
You can save the contents of line
1with%saveusing:Help on
%saveis available with:You can
%edita function body with:After
%edit-ing your functionfunc1, you can get the following output from IPython using_:Then, you can define or re-define a
%macrowith the updatefunc1contents like this:Finally, you can save the new version of
func1with%saveand the new%macrolike this:Hope that clarifies.