1

I saw a function like bellow, I don't know the _ meaning.

def child_handler(signum, _):
    logging.warn('received SIGQUIT, doing graceful shutting down..')

what's the _ there?


but, however, if we ignore the _, why we need there an ignored param?

user7693832
  • 6,119
  • 19
  • 63
  • 114

3 Answers3

1

the _ variable is just a way to say that it's not going to be used further down the line.

Basically, you don't care what the name is because it'll never be referenced.

Nordle
  • 2,915
  • 3
  • 16
  • 34
0

It essentially is a way of ignoring the value of the variable, and we don't want to use it down the line.

Another way of thinking is that it is a placeholder for the value that will be ignored

def func(a, b):

    return a,b

#I care about both return values
c,d = func(2,3)

#I don't care about the second returned value, so I put a _ to ignore it
c, _ = func(2, 3)

Another good use case of this is when you are running a for loop, but don't care about the index.

for _ in range(10):
    #do stuff

Even for functions, it acts like a don't care variable

def func(a, _):
    print(a)

func(1, 5)

The output will be 1

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
-1

The underscore _ in Python can be used for different cases. In this case it means that the second argument of the child_handler function is ignored (so-called "Don't care").

funie200
  • 3,688
  • 5
  • 21
  • 34