Why doesn't QtConsole echo next()?

I found this question about iterator behavior in Python:

Python list iterator behavior and next(iterator)

When I typed in the code:

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

into the jupyter-qtconsole it returned:

0
2
4
6
8

exactly as Martijn Pieters said it should when the interpreter doesn't echo the call to next(a) .

However, when I ran the same the code again in my Bash interpreter and IDLE, the code printed:

0
1
2
3
4
5
6
7
8
9

to the console.

I ran the code:

import platform
platform.python_implementation()

in all three environments and they all said I ran 'CPython' .

So why does the QtConsole suppress the next(a) call when IDLE and Bash don't?

If it helps, I'm running Python 2.7.9 on Mac OSX and using the Anaconda distribution.


This is just a choice the developers of IPython (on which the QtConsole is based) made regarding what should be echoed back to the user.

Specifically, in the InteractiveShell class that is used, function run_ast_nodes is, by default, defined with an interactivity='last_expr' . The documentation on this attribute states:

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.

As you can see: expressions in loops or other blocks are not displayed .

You can change this in the config files for IPython and get it to work like your repl if you really need to. Point is, it was just a preference the designers made.

链接地址: http://www.djcxy.com/p/35726.html

上一篇: Java中的case语句

下一篇: 为什么不QtConsole回声next()?