Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重新运行ipython cpaste代码块_Python_Shell_Ipython - Fatal编程技术网

重新运行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