Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 207
Jack St. Claire Avatar asked Aug 21 '16 19:08

Jack St. Claire


1 Answers

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.

like image 83
Dimitris Fasarakis Hilliard Avatar answered Sep 24 '22 02:09

Dimitris Fasarakis Hilliard