从Python中的函数返回多个值的最佳方法是什么?

时间:2023-01-14 11:17:38

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.

在C#中,我会为字符串使用out参数,但Python中没有等效参数。我仍然是Python的新手,我唯一能想到的是返回一个带有布尔值和修改后的字符串的元组。

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

相关问题:函数返回多个值是pythonic吗?

5 个解决方案

#1


120  

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

succeeded, b = f("a") # Automatic tuple unpacking

#2


28  

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.

但更直接的是,如果你抛出异常,你就迫使他们处理这个问题。如果您尝试返回一个指示失败的值,那么很可能有人无法检查该值并最终导致一些可能难以调试的错误。

#3


15  

Return a tuple.

返回一个元组。

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

success, modified_string = f(something)

#4


7  

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

返回元组是在Python中执行此操作的常用方法。

#5


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.

抛出失败的异常是一种很好的方法,如果你返回了很多不同的值,你可以返回一个元组。对于您引用的特定情况,我经常采用中间方法:成功返回修改后的字符串,失败时返回None。我已经足够了一个未经构造的C程序员,想要在失败时返回一个指向char的NULL指针。

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.

如果我正在编写一个例程来用作更大的库的一部分并被其他开发人员使用,那么我会在失败时抛出异常。当我吃自己的狗食时,我可能会返回不同类型并在返回时进行测试。

#1


120  

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

succeeded, b = f("a") # Automatic tuple unpacking

#2


28  

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.

但更直接的是,如果你抛出异常,你就迫使他们处理这个问题。如果您尝试返回一个指示失败的值,那么很可能有人无法检查该值并最终导致一些可能难以调试的错误。

#3


15  

Return a tuple.

返回一个元组。

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

success, modified_string = f(something)

#4


7  

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

返回元组是在Python中执行此操作的常用方法。

#5


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.

抛出失败的异常是一种很好的方法,如果你返回了很多不同的值,你可以返回一个元组。对于您引用的特定情况,我经常采用中间方法:成功返回修改后的字符串,失败时返回None。我已经足够了一个未经构造的C程序员,想要在失败时返回一个指向char的NULL指针。

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.

如果我正在编写一个例程来用作更大的库的一部分并被其他开发人员使用,那么我会在失败时抛出异常。当我吃自己的狗食时,我可能会返回不同类型并在返回时进行测试。