What does `exit` keyword do in Python3 with Jupyter Notebook?
我目前正在Jupyter笔记本中使用python3,我刚刚遇到一个关键字
| 1 2 3 4 | with open("some_file.txt") as f: for lines in f: print(lines) exit | 
循环中的
通常情况下,单独一行的
| 1 2 | >>> exit Use exit() or Ctrl-D (i.e. EOF) to exit | 
伊普生做了一些不同的事情。IPython为了交互方便而拥有的许多额外系统中,有一个系统可以自动调用某种类型的实例,即
在IPython中,
| 1 2 | In [1]: exit [IPython dies here] | 
。
Jupyter笔记本的ipython内核将
但是,只有当引用autocalable对象的行是单元格的整个内容时,此功能才会触发。在一个循环中,autocall功能不会触发,所以我们将回到什么都没有发生的状态。
事实上,与正常交互模式相比,在正常的非IPython交互会话中,由于IPython和常规交互模式处理表达式自动打印的方式不同,此循环将在每次迭代中打印"use exit()…"消息。
当条件语句的循环或分支中的ipython中使用
| 1 2 3 4 5 6 7 8 9 | for i in range(10):  exit print(i) # 9 if i==9: exit print(exit) # <IPython.core.autocall.ExitAutocall object at 0x7f76ad78a4a8> | 
它不会重新启动内核:
| 1 2 | print(i) # 9 | 
。
但是,当单独在命令行上使用时,它被视为一种魔力(尽管没有
在我的简单测试中,单元1
结果
| 1 2 3 4 5 6 | --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-3f786850e387> in <module> ----> 1 a NameError: name 'a' is not defined | 
然而,有趣的是,似乎也有一个参数可以用来修改这种行为。
测试2:单元1
编辑:看起来@user2357112的答案会填充缺失的部分。伊迪丝2:实际上,这似乎是一个
| 1 2 3 4 5 6 |  class IPython.core.autocall.ZMQExitAutocall(ip=None) Bases: IPython.core.autocall.ExitAutocall Exit IPython. Autocallable, so it needn’t be explicitly called. Parameters: keep_kernel (bool) – If True, leave the kernel alive. Otherwise, tell the kernel to exit too (default). | 
号