使用%save可以保存第1行的内容,方法是:In [2]: %save func1.py 1
The following commands were written to file `func1.py`:
def func1():
pass
有关%save的帮助可通过以下方式获得:In [2]: %save?
Type: Magic function
...
Docstring:
Save a set of lines or a macro to a given filename.
Usage:
%save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
您可以使用%edit函数体:In [3] %edit func1
done. Executing edited code...
在%edit-使用函数func1之后,可以使用_从IPython获得以下输出:In [4]: _
Out[4]: 'def func1():\n print "Hello World"\n\n'
然后,您可以使用如下更新内容定义或重新定义%macro:In [5]: %macro new_func1_macro _
Macro `new_func1_macro` created. To execute, type its name (without quotes).
=== Macro contents: ===
def func1():
print "Hello World"
最后,您可以使用%save和新的%macro保存新版本的func1,如下所示:In [6]: %save func1.py new_func1_macro
The following commands were written to file `func1.py`:
def func1():
print "Hello World"
希望能澄清。