Skip to content Skip to sidebar Skip to footer

Why Does Typing _ In The Python Interpreter Return True?

I am getting very weird interpreter behaviour: >>> _ True >>> type(True) >>> type(_) I tried this because _ ca

Solution 1:

_ will be the result of the last evaluated command - at interpreter start up there isn't any so you'll get a NameError... after that, you'll get the previous result... Try opening a new interpreter and doing 2 + 2... you'll see 4 returned, then type _... eg:

>>> _

Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    _
NameError: name '_'isnot defined
>>> 2 + 24>>> _
4

Solution 2:

2 + 1Out[19]: 3_ + 3Out[20]: 6

_ stores the last returned value. Try it out.

Solution 3:

_ simply gives you the last result evaluated (in the REPL, not in an ordinary script). This can also mysteriously prevent objects from being deleted immediately.

Solution 4:

_ in the interactive interperter is usually the last output you received.

>>>1 + 1
2
>>>_
2

Note it only applies to outputs (printed data won't do).

Post a Comment for "Why Does Typing _ In The Python Interpreter Return True?"