Is it possible to write single line return statement with if statement?

前端 未结 4 999
迷失自我
迷失自我 2020-12-09 07:36

Is is possible to return from a method in single line in python

Looking for something like this

return None if x is None

Tried abov

4条回答
  •  忘掉有多难
    2020-12-09 08:02

    You could also try the list[bool] expression:

    return [value, None][x == None]
    

    Now if the second bracket evaluates to true, None is returned otherwise, the value you want to return is returned

提交回复
热议问题