1

如果满足某些条件,我想停止在 Google Datalab 笔记本上执行执行 python 命令的单元格。

不影响笔记本其余部分的首选方法是什么?

if x:
   quit()

会使笔记本崩溃。

4

1 回答 1

0

One potential solution is to wrap your code in a function and use return to exit early.

def do_work():
  stopExecution = True
  if stopExecution:
     return

  print 'do not print'

do_work()

Another solution is to raise an exception:

stopExecution = True
if stopExecution:
   raise Exception('Done')

print 'do not print'

A better solution is to use the if statement to allow code execution, rather than block it. For example,

if ShouldIContinueWorking():
   doWork()
else:
   print 'Done' # do nothing (preferred) or return from function
于 2016-04-01T00:49:40.130 回答