24

When looking through Python code on GitHub, I've seen several examples of a return with no value. For example:

    if hasattr(self, 'moved_away'):
        return
    # and here code continues

What does the empty return mean?

Mike P
  • 697
  • 13
  • 26
micgeronimo
  • 1,899
  • 5
  • 20
  • 40

2 Answers2

44

It means it will return None. You could remove the return and it would still return None because all functions that don't specify a return value in python will by default return None.

In this particular case it means the code will go no further if the object has the attribute 'moved_away', without the return any code below would be evaluated even if the if statement evaluated to True.

So you can think of it as being similar to a break statement in a loop when you have a condition you want to exit the loop on, without the break the code would continue to be evaluated.

if hasattr(self, 'moved_away'): # if this is True we return/end the function
        return
     # if previous statement was False we start executing code from here
Padraic Cunningham
  • 168,988
  • 22
  • 228
  • 312
9

return exits the current function.

So, here it will stop the execution & return None.

wjandrea
  • 23,210
  • 7
  • 49
  • 68
Hare Kumar
  • 689
  • 7
  • 23