beoliver beoliver - 1 year ago 66
Python Question

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

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

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

This returns none if
foo_input is > 100
. But could it actually 'not' return anything? or does a function allways have to return something?

Answer Source

Functions always return something (at least None, when no return-statement was reached during execution and the end of the function is reached).

Another case is when they are interrupted by exceptions. In this case exception handling will "dominate over the stack" and you will return to the appropriate except or get some nasty error :)

Regarding your problem I must say there are two possibilities: Either you have something to return or you do not have.

  • If you have something to return then do so, if not then don't.
  • If you rely on something being returned that has a certain type but you cannot return anything meaningful of this type then None will tell the caller that this was the case ( There is no better way to tell the caller that "nothing" is returned then by None, so check for it and you will be fine)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download