为什么 QtConsole 不回显 next()?

Why doesn't QtConsole echo next()?

我在 Python 中发现了这个关于迭代器行为的问题:

Python list iterator behavior and next(iterator)

当我输入代码时:

a = iter(list(range(10)))
for i in a:
    print a
    next(a)

进入 jupyter-qtconsole 它返回:

0
2
4
6
8

正如 Martijn Pieters 所说,当解释器不响应对 next(a) 的调用时应该这样做。

但是,当我 运行 在我的 Bash 解释器和 IDLE 中再次使用相同的代码时,打印的代码:

0
1
2
3
4
5
6
7
8
9

到控制台。

我运行代码:

import platform
platform.python_implementation()

三个环境都说我运行'CPython'.

那么,为什么 QtConsole 在 IDLE 而 Bash 没有时抑制 next(a) 调用?

如果有帮助,我是 运行 Python 2.7.9 Mac OSX 并使用 Anaconda 发行版。

这只是 IPythonQtConsole 所基于的)的开发人员针对应向用户回显的内容所做的选择。

具体来说,在 InteractiveShell class that is used, function run_ast_nodes 中,默认情况下,用 interactivity='last_expr' 定义。有关此属性的文档指出:

interactivity : str
  'all', 'last', 'last_expr' or 'none', specifying which nodes should be
  run interactively (displaying output from expressions). 'last_expr'
  will run the last node interactively only if it is an expression (i.e.
  expressions in loops or other blocks are not displayed. Other values
  for this parameter will raise a ValueError.

如您所见:不显示循环或其他块中的表达式

如果确实需要,您可以在 IPython 的配置文件中更改它,并使其像 repl 一样工作。关键是,这只是设计师的偏好。