返回值和参数收集
返回值
return语句
- 中断函数执行并将对象返回到函数调用位置
- 如果没有写return语句,则默认为 return None
return可以返回一个对象,也可以返回多个对象
x,y = foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
x,y,z = foo()
x
1
y
2
z
3
a = foo()
a
(1, 2, 3)
参数收集
星号的作用

def foo(x, *args):
... print('x=',x)
... print('args=',args)
...
foo(2,3,4,5)
x= 2
args= (3, 4, 5)
def bar(**kwargs):
... print('kwargs=',kwargs)
...
bar(1,2,3,)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bar() takes 0 positional arguments but 3 were given
bar(a=1,b=2,c=3)
kwargs= {'a': 1, 'b': 2, 'c': 3}
x,*y=1,2,3,4
x
1
y
[2, 3, 4]
网友评论