重新运行ipython cpaste代码块
我运行了重新运行ipython cpaste代码块,python,shell,ipython,Python,Shell,Ipython,我运行了%cpaste foo并保存了代码块。我可以通过在shell上键入foo来打印代码块 如何重新运行这段代码?我尝试了exec('foo'),但它不起作用。%cpaste-r将重新运行您粘贴到其中的最后一个代码块。 请记住,如果函数在块中,您必须实际执行它,而不仅仅是defit。如果是即时操作,它将返回结果 这实际上是可行的,除了从未执行过foo In [24]: %cpaste Pasting code; enter '--' alone on the line to stop or u
%cpaste foo
并保存了代码块。我可以通过在shell上键入foo
来打印代码块
如何重新运行这段代码?我尝试了
exec('foo')
,但它不起作用。%cpaste-r
将重新运行您粘贴到其中的最后一个代码块。
请记住,如果函数在块中,您必须实际执行它,而不仅仅是def
it。如果是即时操作,它将返回结果
这实际上是可行的,除了从未执行过foo
In [24]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def foo():
: print("bar")
:--
In [25]: %cpaste -r
Re-executing 'def foo():...' (30 chars) #foo is never called, so there's no result shown
这同样有效,并且执行foo,因此您可以返回结果
In [26]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def foo():
: print("bar")
:foo()
:--
bar #this is the result of immediate execution
In [27]: %cpaste -r
Re-executing 'def foo():...' (36 chars)
bar #this is re-runned result