29

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
  • 742
  • 11
  • 26
micgeronimo
  • 2,069
  • 5
  • 23
  • 44

2 Answers2

51

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
  • 176,452
  • 29
  • 245
  • 321
9

return exits the current function.

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

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Hare Kumar
  • 699
  • 7
  • 23