On 28/06/13 14:18, Jim Mooney wrote:
What's the Pythonic standard on multiple returns from a function? It seems easiest to just return from the point where the function fails or succeeds, even it that's multiple points. Or is it considered best to defer everything to one return at the end?
The first. Python functions have one entry point, the top of the function. They can have multiple exit points, anywhere you have a return statement. Languages like Pascal enforce a single exit point, which means you end up writing rubbish code like this: # Using Python syntax instead of Pascal def function(arg): done = False result = some_calculation(arg) if condition(): done = True if not done: result = more_calculations() if condition(): done = True if not done: result = even_more_calculations() if condition(): done = True if not done: result = are_we_done_yet() return result compared to: def function(arg): result = some_calculation(arg) if condition(): return result result = more_calculations() if condition(): return result result = even_more_calculations() if condition(): return result return are_we_done_yet() -- Steven _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor