ipythonspydercpython

Spyder console: "_" stops taking last result after being assigned to


I find it useful for _ to contain the value fo the last result. However, I sometimes want to use _ to suppress the printing of the return argument of a function call. Unfortunately, once _ has been assigned to, it stops taking on the value of the last result:

2+3 # Console output: 5
_ # Console output: 5

_=3+4 # Explicitly assign to "_"
_ # Console output: 7

4+5 # Console output: 9
_ # Console output: 7

In the last line, the default behaviour is for _ to contain the last result 9. However, it retains the 2nd last result instead because it was explicitly assigned to. Let's refer to this as a non-default behaviour, i.e. retaining the value explicitly assigned to _ rather than _ storing the result of the last evaluated expression that was not assigned to a variable.

(1) How else can one selectively suppress console output besides assigning it to a throw-away variable like _? I can assign to another variable (say) dum, but I don't want to constantly issue del dum to keep the Variable Explorer clean and memory free.

(2) Also, once I've explicitly assigned to _, is there any way to get back the default behaviour of having it take on the last result? Issuing del _ doesn't do so.

Note: My previous Q&A simply recognizes the non-default behaviour as the explanation for an error. It doesn't provide a solution.


Solution

  • (1) To suppress console output in IPython, you can add a semicolon

    In [1]: 3+1;
    In [2]:
    

    (2) In Python shell, setting _ would stop python from setting it to the last result, del _ would revert this as expected.

    But in IPython shell, if any of the variables _, __, ___ are "out-of-sync", they won't be set to the last 3 results. So to revert any change of _, need to del _,__,___.