python - can lambda have more than one return

丶灬走出姿态 提交于 2019-12-03 08:19:56

问题


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

回答1:


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



回答2:


Sure:

lambda a, b: (a + 1, b * 1)



回答3:


what about:

lambda a,b: (a+1,b*1)


来源:https://stackoverflow.com/questions/16674004/python-can-lambda-have-more-than-one-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!