is it possible to not return anything from a function in python?

前端 未结 5 601
夕颜
夕颜 2020-12-01 16:09

with a simple filter that test input against a range 0-100.

def foo(foo_input):
    if 0 <= foo_input <= 100:
        return f_input

5条回答
  •  渐次进展
    2020-12-01 16:50

    I sort of like the implicit return None but pylint flags it as bad style, warning:

    Either all return statements in a function should return an expression, or none of them should.pylint(inconsistent-return-statements)

    Hence,

    def foo(foo_input):
        if 0 <= foo_input <= 100:
            return f_input
        return None
    

    might be better style, even if they are functionally the same.


    More info available here, where the Pylint change-log states:

    A new Python checker was added to warn about inconsistent-return-statements. A function or a method has inconsistent return statements if it returns both explicit and implicit values ...

    According to PEP8, if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function (if reachable).

提交回复
热议问题