gpt4 book ai didi

python - 为什么 QtConsole 不回显 next()?

转载 作者:太空狗 更新时间:2023-10-29 21:54:28 28 4
gpt4 key购买 nike

我发现了这个关于 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 会抑制 next(a) 调用而 IDLE 和 Bash 不会?

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

最佳答案

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

具体来说,在 InteractiveShell使用的类,函数 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.

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

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

关于python - 为什么 QtConsole 不回显 next()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39068174/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com