2

When I started to code with python, a senior developer was advising me to put a return True (boolean value in general) whenever the scope of a function ends.

so for example:

def mytest():
    try:
        os.system('convert ./text.pdf ./text.jpg')
    except:
        pass
   
    return True

Still, does it matter to the python interpreter? Is returning None equivalent to not returning at all?

borgr
  • 20,175
  • 6
  • 25
  • 35
Braiano
  • 335
  • 6
  • 26
  • It doesn't matter for the interpreter – בנימין כהן Feb 18 '21 at 11:50
  • it dont matter, it's sometimes used to verify a function has been run but it's more popular in Java/OOP – mightyandweakcoder Feb 18 '21 at 11:51
  • 5
    If a senior developer advised you to do this, then either you misunderstood their advice, or (less likely) they don't know what they're talking about. – kaya3 Feb 18 '21 at 11:53
  • 3
    That's a bad advice. Function should return what it is supposed to return (including nothing i.e. void i.e. `None`). It all depends on the function. – Mustafa Aydın Feb 18 '21 at 11:53
  • 1
    Python functions have an an implicit ``return None`` at their end. There is no need to add a ``return`` if you don't need one. – MisterMiyagi Feb 18 '21 at 11:53
  • 1
    I can imagine that the developer advised you to `return True` in case the command succeeds and `return False` in case the `except` block is entered; having `except: pass` here means the function fails silently and you have no way of knowing whether the conversion succeeded. – kaya3 Feb 18 '21 at 11:54
  • 1
    I've never heard of such advice. If you heard that, it is bad advice. You can `return True` in *specific cases* where it makes sense to. Its perfectly valid for a function not to return anything at all (it implicitly returns `None` in such a case). It is silly & redundant to have an explicit return statement in the above case. – costaparas Feb 18 '21 at 11:56
  • 1
    The only reason I can think of to have a `return True` at the end of a function which can never return anything else, is if the function is to be passed as a callback to some API where the callback has to return a boolean value indicating something (e.g. success or failure) which is irrelevant to the specific callback function you're writing (e.g. if it always succeeds, then it should always return `True`). – kaya3 Feb 18 '21 at 11:59
  • I have heard of this advice before, but it's bad advice. The logic goes that the boolean tells you that the function did not error. However, you should be using an exception (or lack thereof) to know whether a (command-like) function has succeeded. – D Hudson Feb 18 '21 at 12:19
  • Not sure if a duplicate, but surely related reading material (with other useful links as duplicates): [Does every Python function have to return at the end?](https://stackoverflow.com/questions/42714625/does-every-python-function-have-to-return-at-the-end) – Tomerikoo Feb 18 '21 at 12:24

1 Answers1

0

There is no requirement for an explicit return – a Python function will implicitly return None when it ends without a value.

>>> def void():
...     a = 3 + 4
...
>>> print(void())
None

As for what is advisable, per the official style guide it is idiomatic to be consistent with return statements:

Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. 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):

Note how an explicit return statement at the end of the function should only be used if the function may return some value other than the implied None. There are basically three cases:

  • The function returns never. Do not add return at the end.
  • The function returns early but without an explicit value. Do not add return at the end.
  • The function returns at least one explicit value. Do add return with an explicit value at the end.

As an example, it would make sense for the function to signal success or failure:

def mytest():
    return_code = os.system('convert ./text.pdf ./text.jpg')
    # demonstration only – use `return return_code == 0` in practice
    if return_code == 0:
        return True  # explicit return of a value
    return False     # -> explicit return at the end
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • 1
    It would "make sense" -- sure, technically you *could* do that. But wouldn't it make more sense to have the function `raise` an exception? – costaparas Feb 18 '21 at 12:07
  • @costaparas Both indicating success or bubbling up the original exception is sensible depending on how the function is supposed to be used. Remember that boolean checks are semantically much lighter than ``try:except:`` handler. Handling the exception inside the function gives a clear contract to the outside, without requiring the caller to know the possible exceptions. – MisterMiyagi Feb 18 '21 at 12:11