您可以返回要作为元组返回的值.
例:
>>> def f():
... return 1, 2, 3
...
>>> a, b, c = f()
>>> a
1
>>> b
2
>>> c
3
>>>
Run Code Online (Sandbox Code Playgroud)
Python有一些参数解包很酷.因此,虽然您只能返回一个值,但如果它是一个元组,您可以自动解压缩它:
>>> def foo():
... return 1, 2, 3, 4 # Returns a tuple
>>> foo()
(1, 2, 3, 4)
>>> a, b, c, d = foo()
>>> a
1
>>> b
2
>>> c
3
>>> d
4
Run Code Online (Sandbox Code Playgroud)
在Python 3中,您有更多高级功能:
>>> a, *b = foo()
>>> a
1
>>> b
[2, 3, 4]
>>> *a, b = foo()
>>> a
[1, 2, 3]
>>> b
4
>>> a, *b, c = foo()
>>> a
1
>>> b
[2, 3]
>>> c
4
Run Code Online (Sandbox Code Playgroud)
但这在Python 2中不起作用.