12

I was reading Hidden features of Python and I came across this answer.

Right from the post:

When using the interactive shell, "_" contains the value of the last printed item:

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> _
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

What is the name of this operator ? I cannot find it on the document and I've never heard of it (as well as in other languages). Is it worth using it?

PS. I want to know its name because I want to see how the function is implemented and to search if other languages have this awesome function.

Community
  • 1
  • 1
Thanakron Tandavas
  • 5,615
  • 5
  • 28
  • 41

2 Answers2

12

It's neither an operator nor a function. It's a variable that automatically gets assigned the result of each expression executed by the shell.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

It looks like that character has a couple functions. It is used preceding global variables so they are not mixed in with regular variables. It can also be used in a loop as a throwaway to indicate that the variable is not going to be used.

In this case, underscore (_) when typed into the interpreter will return the value of the last executed statement.

For more info

What is the purpose of the single underscore "_" variable in Python?

What is the meaning of a single- and a double-underscore before an object name?

the underscore of python

Community
  • 1
  • 1
jfa
  • 1,047
  • 3
  • 13
  • 39
  • 3
    "*It is used preceding global variables so they are not mixed in with regular variables*". That is not "use" of a variable. –  Apr 07 '13 at 21:03