tags:

views:

53

answers:

2
>>> type(_)
<type 'ellipsis'>

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

what's the usefulness of this _ function?

+2  A: 

It just makes it easier to track intermediate values or to operate on the previously returned value.

>>> [x*x for x in range(5)]
[0, 1, 4, 9, 16]
>>> sum(_) # instead of having to type sum([0,1,4,9,16]) by hand
30
Mark Rushakoff
so, its not better declare a variable var = [x*x for x in range(5)] sum(var) instead use _?
killown
i should agree that its usefull when you are using the interpreter as calculator but for test pieces of code i think its a bad pratice.
killown
The idea is to use it for quick interactive calculations such as this.
Andrew Jaffe
@killown: The point is that it's interactive. Sure, in thought-out production code, you would use proper variables, but at the REPL, you sometimes call `GetAFoo()` and only once you have received its value are you aware that you need to call its `.Bar()` method. This is the point of the `_` in interactive Python.
Mark Rushakoff
`_` is *only defined in interactive mode*.
THC4k
A: 

in case you use ipython it's part of ipythons [output caching system] - it just stores the previous output.

edit: oh, it seems to be implemented for the default python interpreter as well.

deif