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

前端 未结 4 985
迷失自我
迷失自我 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:22

    Yes, it's called a conditional expression:

    return None if x is None else something_else
    

    You need an else something in a conditional for it to work.

提交回复
热议问题