Shengjie Shengjie - 3 months ago 6x
Python Question

python - can lambda have more than one return

I know lambda doesn't have a return expression. Normally

def one_return(a):
#logic is here
c = a + 1
return c

can be written:

lambda a : a + 1

How about write this one in a lambda function:

def two_returns(a, b):
# logic is here
c = a + 1
d = b * 1
return c, d


Yes, it's possible. Because an expression such as this at the end of a function:

return a, b

Is equivalent to this:

return (a, b)

And there, you're really returning a single value: a tuple which happens to have two elements. So it's ok to have a lambda return a tuple, because it's a single value:

lambda a, b: (a, b) # here the return is implicit