[IPython-dev] Execution semantics and how to set them

Thomas Kluyver takowl at gmail.com
Sun Jan 25 13:57:01 EST 2015


Hi Zaki,

On 25 January 2015 at 10:26, Zakariyya Mughal <zaki.mughal at gmail.com> wrote:

>
> I'm a language kernel author  and I'm looking at the execution semantics
> document[^1] where I see a reference to the "single" execution mode.
>
> It says that the code
>
>     for i in range(10):
>         i**2
>
> running in "single" mode would display 10 lines.
>
> I looked through the IPython source and I see that the execution
> semantics are set as an argument for `run_ast_nodes`[^2], but I see no
> way that the given for-loop would give multiple outputs unless the
> `interactivity` parameter is set to `last` or `all`.


First, let me emphasise that all of this is only relevant for executing
Python code. If you're building a kernel for another language, you should
ignore all of this. That documentation page is only about IPython's own
kernel.

The three modes described on that page (exec, eval, single) are compilation
modes of Python itself, passed as arguments to the built-in compile()
function.

The parameter passed to our run_ast_nodes() method (which can be all, none,
last or last_expr) controls which of a list of AST nodes are compiled in
'single' mode. Our default of last_expr is chosen specifically so that the
for loop example above does not produce output, because we think that's
more likely to be annoying than useful. There is no way to control this
from the frontend, and I don't think we have any intention to add such a
feature.

For your own kernel, you should implement whatever display of output makes
sense given the language and the likely users. For Python, we decided that
it makes sense to automatically display the result of the last statement if
that statement is an expression, and implemented that using the details
described above. For R, I implemented similar behaviour based on the
evaluate package by creating an output_handler with a value= parameter.

Which language are you working on a kernel for?

Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150125/2c39eb42/attachment.html>


More information about the IPython-dev mailing list