本文介绍了从函数返回多个值的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个函数,我需要对字符串做一些事情.我需要函数返回一个布尔值,指示操作是否成功,我还需要返回修改后的字符串.
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.
在 C# 中,我会为字符串使用 out 参数,但在 Python 中没有等价物.我对 Python 还是很陌生,我唯一能想到的就是返回一个带有布尔值和修改过的字符串的元组.
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.
推荐答案
def f(in_str):
out_str = in_str.upper()
return True, out_str # Creates tuple automatically
succeeded, b = f("a") # Automatic tuple unpacking
这篇关于从函数返回多个值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!