Jul-24-2018, 02:14 AM
I've been wondering about the best option between multiple return statements and the use of an intermediary result variable. This of course in the case of conditional blocks within a function.
I remember that in the little C I took they were passionate about only ever using one single return statement at the end of your function, but in Python I've seen some code with multiple returns.
Is there a convention that states which choice is cleverer in Python? Or some maintenance/readability concern?
For my fellow beginners, here are concrete examples of what I'm talking about (I know this function is too short and not enough detailed to be worth anything, just trying to illustrate my question):
multiple returns
I remember that in the little C I took they were passionate about only ever using one single return statement at the end of your function, but in Python I've seen some code with multiple returns.
Is there a convention that states which choice is cleverer in Python? Or some maintenance/readability concern?
For my fellow beginners, here are concrete examples of what I'm talking about (I know this function is too short and not enough detailed to be worth anything, just trying to illustrate my question):
multiple returns
def even_or_not(number): """ boolean type function, returns T if number given in arg is even, F otherwise """ if number % 2 == 0: print("Even") return True else: print("Odd") return Falseuse of result variable
def even_or_not(number): """ boolean type function, returns T if number given in arg is even, F otherwise """ if number % 2 == 0: print("Even") result = True else: print("Odd") result = False return result