It's messed up.
Suddenly, I did a little research on Python's return
.
There is no difference in behavior, but it seems that there is a flow to use which return
.
return
is used to break out of the loop.
For example, if you have only one golden apple out of 20 and you want to find it
for apple in apples:
if "golden" in apple.color:
print "I found it!"
return
It will be like this. There is only one golden apple, so it's okay to break out of the loop when you find it. In this case, just use return
.
return None
return None
is used when the returned value is used later.
For example, when you want to return the price of a golden apple
if is_golden(apple):
return apple.price
else:
return None
It will be. For non-golden apples, no price information is needed, so None
is returned.
If you don't need to use the returned value later, you don't need to write return
.
if is_golden(apple):
print "It's a golden apple!"
In this case, there is no need to return (the returned value is not reused), so the return
description is unnecessary.
--return
is when exiting the loop
--return None
is when the return value is reused
--No need return
if the return value is not reused