>>> def multi_sum(*args):
s = 0
for item in args:
s += item
return s
>>> multi_sum(3,4,5)
12
>>> multi_sum(3,4)
7
def do_something(name, age, gender='男', *args, **kwds): print('姓名:%s,年龄:%d,性别:%s'%(name, age, gender)) print(args) print(kwds) ============================== do_something('xufive', 50, '男', 175, 75, math=99, english=90) 姓名:xufive,年龄:50,性别:男 (175, 75) {'math': 99, 'english': 90}
============================此外,一颗星和两颗星还可用于列表、元组、字典的解包,看起来更像C语言:
>>> a = (1,2,3) >>> print(a) (1, 2, 3) >>> print(*a) 1 2 3 >>> b = [1,2,3] >>> print(b) [1, 2, 3] >>> print(*b) 1 2 3 >>> c = {'name':'xufive', 'age':51} >>> print(c) {'name': 'xufive', 'age': 51} >>> print(*c) name age >>> print('name:{name}, age:{age}'.format(**c)) name:xufive, age:51