我有一个函数,我需要对字符串做一些事情。我需要该函数返回一个指示操作是否成功的 bool 值,并且我还需要返回修改后的字符串。
在 C# 中,我会为字符串使用 out 参数,但在 Python 中没有等效参数。我对 Python 还是很陌生,我唯一能想到的就是返回一个带有 bool 值和修改后的字符串的元组。
相关问题:Is it pythonic for a function to return multiple values?
最佳答案
def f(in_str):
out_str = in_str.upper()
return True, out_str # Creates tuple automatically
succeeded, b = f("a") # Automatic tuple unpacking
关于python - 从函数返回多个值的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38508/