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

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

    It is possible to write a standard "if" statement on a single line:

    if x is None: return None
    

    However the pep 8 style guide recommends against doing this:

    Compound statements (multiple statements on the same line) are generally discouraged

提交回复
热议问题