我有一个函数,大多数时候应该返回一个值,但有时我需要从函数返回的第二个值.
Here我发现了如何返回多个值,但是大多数时候我只需要其中一个值,我想写这样的东西:
def test_fun():
return 1,2
def test_call():
x = test_fun()
print x
但调用此结果
>>> test_call()
(1,2)
当试图返回两个以上时,如
def test_fun2():
return 1,2,3
def test_call2():
x,y = test_fun2()
print x,y
我收到一个错误
Traceback (most recent call last):
File "", line 1, in
File "my_module.py", line 47, in test_call2
x,y = test_fun2()
ValueError: too many values to unpack
我正在考虑像matlab这样的东西,其中x = test_fun()会导致x == 1(而[x y] = test_fun()也会按预期工作).在python中有类似的东西吗?
Python多值返回解析

2148

被折叠的 条评论
为什么被折叠?



