Pollo
Pollo

Reputation: 275

IPython / Spyder do not show output of the assignment of a variable

I am using Spyder to write scientific Python code but do not know if the following question is really a Spyder question or an IPython one...

If I create a variable (I have simple type in mind, like doubles, ndarrays and so on), its value is not shown just after creation.

So for example if I do

s = x1 + x2

I would like to see (or having the possibility of choosing whether I want to see it or not) the value of the newly created s. With these settings, I would need to type

s

just after assignment to see the result.

FYI, the behavoir I am looking for is achieved in the Matlab console through the presence or not of a ;

Just to be clear: I am asking this question to save typing s (but sometimes the name is longer) and an Enter stroke. So you can call me lazy if you like.

Upvotes: 0

Views: 325

Answers (1)

Matt
Matt

Reputation: 27843

This is not how Python works; assignments do not have values.

There is a hidden config option in IPython/Spyder to do that but only with the last assignment of a cell.

In [1]: %config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

In [2]: a = 1+1
Out[2]: 2

you can set it as well in your IPython configuration file, and the values this can take are ['all', 'last', 'last_expr', 'none', 'last_expr_or_assign']

from ipython --help-all

--InteractiveShell.ast_node_interactivity=<Enum>
    'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying which
    nodes should be run interactively (displaying output from expressions).
    Choices: any of ['all', 'last', 'last_expr', 'none', 'last_expr_or_assign']
    Default: 'last_expr'

This might not work in all cases, like tuple assignment, unpacking and co, feel free to open bug reports if you encounter those case.

I would also warn you that the internal way this work in IPython is really tricky so it may have unintended consequences.

Upvotes: 2

Related Questions