在python中管理函数的输出数量

在python中管理函数的输出数量,python,function,io,output,Python,Function,Io,Output,我想做一个返回可变数量输出的函数 我尝试了out1,out2=function()[:-1]和 out1,out2=function()[:2]和 使用if语句更改function() def function(): a=1;b=2;c=3 return a,b,c out1, out2 = function() ### Line number 4 我只想通过更改function()而不使用if语句来执行“行号4”。 out1,out2=function() 当然,这表明 Va

我想做一个返回可变数量输出的函数

我尝试了
out1,out2=function()[:-1]
out1,out2=function()[:2]
和 使用
if
语句更改
function()

def function():
    a=1;b=2;c=3
    return a,b,c
out1, out2 = function() ### Line number 4
我只想通过更改
function()
而不使用
if
语句来执行“行号4”。
out1,out2=function()
当然,这表明
ValueError:要解包的值太多(预期为2)

不要在函数调用中放入多个变量,而应该是:

out1, out2 = function()
这样做:

values = function()
print "I've got", len(values), "return values"

函数只有一个返回值。它可以是一个元组,这就是代码中的元组。由于Python的一些语言特性,看起来有多个返回值。在这种情况下,可以定义一个没有括号和解包的元组。