34

I have a python specific question. What does a single underscore _ as a parameter means? I have a function calling hexdump(_). The _ was never defined, so I guess it has some special value, I could not find a reference telling me what it means on the net. I would be happy if you could tell me.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Steve
  • 349
  • 1
  • 3
  • 3
  • 2
    To people answering this: what does a single underscore as a parameter mean in ("normal") python **code**, *and **not** when used in the interpreter*? – Marco Jan 16 '19 at 09:15

4 Answers4

19

From what I've been able to figure out, it seems like this is the case:

_ is used to indicate that the input variable is a throwaway variable/parameter and thus might be required or expected, but will not be used in the code following it.

For example:

# Ignore a value of specific location/index 
for _ in range(10) 
    print("Test")
  
# Ignore a value when unpacking 
a,b,_,_ = my_method(var1) 

(Credit to this post)

The specific example I came across was this:

    def f(_):
        x = random() * 2 - 1
        y = random() * 2 - 1
        return 1 if x ** 2 + y ** 2 < 1 else 0
Kevin
  • 6,665
  • 1
  • 17
  • 14
Marco
  • 8,958
  • 1
  • 36
  • 56
  • 2
    amazing how I came here for the exact same example (found in https://github.com/apache/spark/blob/master/examples/src/main/python/pi.py) haha – pcko1 May 06 '20 at 12:36
  • Or, as in [this example](https://medium.com/the-dl/how-to-use-pytorch-hooks-5041d777f904), you can do this for a second throway variable by using a double underscore. – jtb Feb 05 '23 at 22:42
13

In Python shells, the underscore (_) means the result of the last evaluated expression in the shell:

>>> 2+3
5
>>> _
5

There's also _2, _3 and so on in IPython but not in the original Python interpreter. It has no special meaning in Python source code as far as I know, so I guess it is defined somewhere in your code if it runs without errors.

Tamás
  • 47,239
  • 12
  • 105
  • 124
  • Why do you think this is not answering the question? In my opinion, it does. – Hagbard Jan 16 '19 at 10:03
  • 1
    `What does a single underscore _ as a parameter means?` - underscore as a function parameter, not as a statement (in interactive interpreter) – Derte Trdelnik Jan 17 '19 at 15:52
  • @DerteTrdelnik It's still a variable, even if it's the entire expression serving as an expression statement. (Also, the original question asks about an *argument* to a function, despite using the word parameter.) – chepner Feb 22 '19 at 04:53
10

underscore is considered a 'don't care' variable, furthermore IDEs like PyCharm will not give a warning for it if it is unused

so in a function

def q(a, b, _, c):
    pass

the IDE will underline a,b and c (unused parameter) but not the underscore

why would you use it and not omit the parameter?

->when you inherit from some class and want to override a function where you don't want to use some parameter

other common use is to indicate you don't want to use a part of a tuple when you iterate (or other unpacking) - this reduces clutter

names_and_food = [('michael', 'fruit'), ('eva', 'vegetables')]
for name, _ in names_and_food:
    print(name)

I cant find it in any python PEP, but pylint has it even in the FAQ

Derte Trdelnik
  • 2,656
  • 1
  • 22
  • 26
3

It doesn't have a special value in the code you write. It stores the result of the last expression you evaluated in your interactive interpreter and is used for convenience

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • 1
    The interpreter you get when you type `python` at the prompt and hit enter. Try typing `3+4` enter at the `>>>` prompt and hitting enter. Then trying printing the value of `_`. You'll get `7` (result of last expression evaluated). – Noufal Ibrahim Apr 26 '11 at 10:15