Python Forum
Multiple "return" statements or "result" variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple "return" statements or "result" variable?
#1
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
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 False
use 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
Reply


Messages In This Thread
Multiple "return" statements or "result" variable? - by WolfWayfarer - Jul-24-2018, 02:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  This result object does not return rows. It has been closed automatically dawid294 6 1,243 Mar-30-2024, 03:08 AM
Last Post: NolaCuriel
  Multiple variable inputs when only one is called for ChrisDall 2 520 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Multiple Loop Statements in a Variable Dexty 1 1,228 May-23-2022, 08:53 AM
Last Post: bowlofred
  how do I return Max Test result + corresponding student name from an excel dataset? sean1 3 1,306 Jan-16-2022, 09:07 PM
Last Post: snippsat
  Function - Return multiple values tester_V 10 4,567 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Multiple Or Statements JoeDainton123 8 2,681 Sep-15-2020, 08:14 PM
Last Post: buran
  Function will not return variable that I think is defined Oldman45 6 3,592 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  Decorators and return statements JonEdward 2 1,928 Jul-24-2020, 05:02 PM
Last Post: JonEdward
  Help creating a variable as a result Realen 2 1,811 Jun-18-2020, 04:46 AM
Last Post: Realen
  How to pass multiple values from one sample to nc variable? Baloch 0 1,882 Jun-01-2020, 09:27 PM
Last Post: Baloch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020