Python: What is the difference between `lambda` and `lambda_`?

Viewed 100

I know the function of lambda: and lambda var: , but what does lambda_: means acutally?

2 Answers

lambda_ is just a variable name, like any other. Like foo or x.

If you saw:

lambda_: Something

Then that is actually a variable annotation, for type hints, so the same as:

num: int
num = 0

lambda: means lambda method doesn't take any argument

#!/usr/bin/env python3.10

def caller(var):
    var()


caller(lambda : print("OP"))

lambda var: means method is taking var as argument

#!/usr/bin/env python3.10

def caller(var,arg):
    var(arg)


foo = lambda x: print(x)
caller(foo, "OP")

lambda _: here _ is an argument

#!/usr/bin/env python3.10

def caller(var,__):
    var(__)


foo = lambda _ : print(_)
caller(foo, "OP")

output for all above program is OP

Related