简体   繁体   English

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

[英]What's the best way to return multiple values from a function?

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.在 C# 中,我会为字符串使用 out 参数,但在 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.我对 Python 还是很陌生,我唯一能想到的就是返回一个带有布尔值和修改过的字符串的元组。

Related question: Is it pythonic for a function to return multiple values?相关问题: 函数返回多个值是pythonic吗?

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

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

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.如果您尝试返回一个指示失败的值,很可能有人无法检查该值并最终导致一些可能难以调试的错误。

Return a tuple.返回一个元组。

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

success, modified_string = f(something)

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

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.对于您引用的特定情况,我经常采用中间方法:成功时返回修改后的字符串,失败时返回 None。 I'm enough of an unreconstructed C programmer to want to return a NULL pointer to char on failure.对于一个未重构的 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.当我吃自己的狗粮时,我可能会返回不同类型的食物并在返回时进行测试。

您可以将 return 语句与多个表达式一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从循环中返回多个数据帧的最佳方法是什么? - What is the best way to return multiple data frames from a loop? 从递归函数返回堆栈变量的最佳方法是什么? - What is the best way to return a stack variable from a recursive function? 从Python中的另一个函数调用一个函数中多个值的最佳方法 - The best way to call multiple values in a function from another function in Python 从可变长度字符串中解析值的最佳方法是什么? - What's the best way to parse values from a variable length string? 从多个目录加载大量图像的最佳方法是什么? - What's the best way to load a lot of images from multiple directories? 以结构化方式从python中的函数返回多个值的替代方法? - Alternative to return multiple values from function in python in a structured way? 从 function 返回多个值的非侵入式方法? - Non-invasive way to return multiple values from a function? 格式化函数的返回值以在变量之间包含空格的最佳方法是什么? - What is the best way to format a function's return value to include spaces between variables? 用单独的Pan​​das系列中的值替换NaN值(在Pandas DataFrame中)的最佳方法是什么? - What's the best way to replace NaN values (in a Pandas DataFrame) with values from a separate Pandas Series? 函数多重返回类型的最佳实践 - best practice for multiple return type from function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM