调用other_function 时需要解压缩这些参数。
other_function(*do_something(lis))
根据错误消息,您的其他函数似乎已定义(并且应该定义为)
def other_function(arg1, arg2):
pass
所以,当您从do_something 返回时,实际上是在返回一个包含(lis[0], lis[1]) 的元组。因此,当您最初调用 other_function 时,您传递了一个元组,而您的 other_function 仍然期待第二个参数。
如果您进一步分解它,您可以看到这一点。以下是在不同处理时返回的外观细分、错误重现和解决方案演示:
返回单个变量将返回结果的元组:
>>> def foo():
... lis = range(10)
... return lis[1], lis[2]
...
>>> result = foo()
>>> result
(1, 2)
返回两个变量,解压到每个变量中:
>>> res1, res2 = foo()
>>> res1
1
>>> res2
2
尝试使用 result 调用 other_function,它现在只包含结果的元组:
>>> def other_function(arg1, arg2):
... print(arg1, arg2)
...
>>> other_function(result)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: other_function() missing 1 required positional argument: 'arg2'
使用 res1、res2 调用 other_function 来保存从 foo 返回的每个值:
>>> other_function(res1, res2)
1 2
使用result(您的元组结果)并在对other_function的函数调用中解包:
>>> other_function(*result)
1 2