78

I have a function where I need to do something to a string. I need the function to return a boolean indicating whether or not the operation succeeded, and I also need to return the modified string.

In C#, I would use an out parameter for the string, but there is no equivalent in Python. I'm still very new to Python and the only thing I can think of is to return a tuple with the boolean and modified string.

Related question: Is it pythonic for a function to return multiple values?

martineau
  • 119,623
  • 25
  • 170
  • 301
John Rutherford
  • 10,704
  • 7
  • 30
  • 32

6 Answers6

136
def f(in_str):
    out_str = in_str.upper()
    return True, out_str # Creates tuple automatically

succeeded, b = f("a") # Automatic tuple unpacking
Svend
  • 7,916
  • 3
  • 30
  • 45
John Millikin
  • 197,344
  • 39
  • 212
  • 226
31

Why not throw an exception if the operation wasn't successful? Personally, I tend to be of the opinion that if you need to return more than one value from a function, you should reconsider if you're doing things the right way or use an object.

But more directly to the point, if you throw an exception, you're forcing them to deal with the problem. If you try to return a value that indicates failure, it's very well possible somebody could not check the value and end up with some potentially hard to debug errors.

Jason Baker
  • 192,085
  • 135
  • 376
  • 510
  • 1
    Agreed. Pythonic functions shouldn't return codes to indicate "success/failure" when one is vastly more likely than the other. Just throw an exception in the exceptional case. That being said, just return a tuple when needed: "return a, b, c" – Dan Lenski Oct 10 '08 at 19:19
  • 8
    I don't like functions that throw exceptions for non-fatal errors. – Seun Osewa Dec 02 '08 at 12:27
  • 4
    When failure is common, returning the failure information in a tuple is a good approach. Example would be a function that you call multiple times with test values to find a "good" value, you expect it to fail many times and eventually find a good value. In that case just "return result, reason" rather than raising an exception. – Graham May 19 '11 at 15:06
17

Return a tuple.

def f(x):
    # do stuff
    return (True, modified_string)

success, modified_string = f(something)
rmmh
  • 6,997
  • 26
  • 37
  • My partner argue with me about weather should add `(` and `)` when want to return multi values. When I write code in Pycharm, the ide shows the wornning at lines where return multi values with `()`, but we can't find the code style rule at pep8. How do you think about it? Thank you. – Vi.Ci Aug 11 '17 at 06:55
7

Returning a tuple is the usual way to do this in Python.

Chris Upchurch
  • 15,297
  • 6
  • 51
  • 66
3

Throwing an exception for failure is one good way to proceed, and if you're returning a lot of different values, you can return a tuple. For the specific case you're citing, I often take an intermediate approach: return the modified string on success, and return None on failure. I'm enough of an unreconstructed C programmer to want to return a NULL pointer to char on failure.

If I were writing a routine to be used as part of a larger library and consumed by other developers, I'd throw an exception on failure. When I'm eating my own dogfood, I'll probably return different types and test on return.

tuxedo
  • 397
  • 1
  • 7
0

You can use return statement with multiple expressions