36

I don't understand what this single underscore means. Is it a magic variable? I can't see it in locals() and globals().

>>> 'abc'
'abc'
>>> len(_)
3
>>> 
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
thebat
  • 2,029
  • 4
  • 21
  • 30
  • Related post - [Finding a list of all double-underscore variables?](https://stackoverflow.com/q/8920341/465053) – RBT Jul 24 '18 at 06:24

3 Answers3

55

In the standard Python REPL, _ represents the last returned value -- at the point where you called len(_), _ was the value 'abc'.

For example:

>>> 10
10
>>> _
10
>>> _ + 5
15
>>> _ + 5
20

This is handled by sys.displayhook, and the _ variable goes in the builtins namespace with things like int and sum, which is why you couldn't find it in globals().

Note that there is no such functionality within Python scripts. In a script, _ has no special meaning and will not be automatically set to the value produced by the previous statement.

Also, beware of reassigning _ in the REPL if you want to use it like above!

>>> _ = "underscore"
>>> 10
10
>>> _ + 5

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    _ + 5
TypeError: cannot concatenate 'str' and 'int' objects

This creates a global variable that hides the _ variable in the built-ins. To undo the assignment (and remove the _ from globals), you'll have to:

>>> del _

then the functionality will be back to normal (the builtins._ will be visible again).

user2357112
  • 260,549
  • 28
  • 431
  • 505
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • 8
    FYI: REPL is short for Read-Eval-Print Loop. As always, wikipedia has more info if you want it. http://en.wikipedia.org/wiki/Read-eval-print_loop – David Locke Oct 08 '09 at 16:31
  • What is a "run-of-the-mill identifier"? Quick search resulted in "merely average; commonplace; mediocre" - what does this mean regarding "_" in python scripts? – user937284 Nov 23 '13 at 21:01
  • FYI - **A word of caution** : The `_` variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behavior. – RBT Jul 24 '18 at 06:22
18

Why you can't see it? It is in __builtins__

>>> __builtins__._ is _
True

So it's neither global nor local. 1

And where does this assignment happen? sys.displayhook:

>>> import sys
>>> help(sys.displayhook)
Help on built-in function displayhook in module sys:

displayhook(...)
    displayhook(object) -> None

    Print an object to sys.stdout and also save it in __builtin__.

1 2012 Edit : I'd call it "superglobal" since __builtin__'s members are available everywhere, in any module.

u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
2

Usually, we are using _ in Python to bind a ugettext function.

Natim
  • 17,274
  • 23
  • 92
  • 150
  • 1
    this is also true, but only for Python Applications. `gettext.install` will bind to `__builtins__._`, so that it is available without importing in all of the application; thus the same kind of "magic" name. – u0b34a0f6ae Oct 08 '09 at 16:29