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

标签 python variables return

我有一个函数,我需要对字符串做一些事情。我需要该函数返回一个指示操作是否成功的 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/

相关文章:

javascript - 使用 Math.round 的奇怪方法结果

python - 使用Python读取矩阵文件(mtx): not enough values to unpack

Python 多处理池 : maxtasksperchild

c++ - 如何将 char 字符显示为 int,而不是 ASCII 符号

mysql - 在 shell 脚本中将表计数设置为变量..?

mysql - 使用 REGEXP 将字符串变量添加到 MySQL SELECT 时是否需要添加引号?

javascript - 如何在另一个 JavaScript 中创建并使用自己的函数

c - C程序不会终止

python - 做一个好公民和网络抓取

python - 如何使用 SqlAlchemy 顺序获取列中的所有值?