Is it ok to skip “return None”?

后端 未结 7 1640
Happy的楠姐
Happy的楠姐 2020-12-03 06:17

I wonder if it is bad manner to skip return None, when it is not needed.

Example:

def foo1(x):
    if [some condition]:
        return B         


        
7条回答
  •  無奈伤痛
    2020-12-03 07:06

    To expound on what others have said, I use a return None if the function is supposed to return a value. In Python, all functions return a value, but often we write functions that only ever return None, because their return value is ignored. In some languages, these would be called procedures.

    So if a function is supposed to return a value, then I make sure all code paths have a return, and that the return has a value, even if it is None.

    If a function "doesn't" return a value, that is, if it is never called by someone using its return value, then it's ok to end without a return, and if I need to return early, I use the bare form, return.

提交回复
热议问题