在调用带有多值参数的函数时,如果希望:(1)将一个元组变量传递给args(2)将一个字典变量传递给keargs。就可以选择拆包,简化参数的传递,拆包的方式是:
def demo(*args, **kwargs):
print(args)
print(kwargs)
gl_nums = (1, 2, 3)
gl_dict = {"name": "小明", "age": 18}
demo(*gl_nums, **gl_dict)
输出:
(1, 2, 3)
{‘name’: ‘小明’, ‘age’: 18}
如果调用函数变量前不使用星号:
def demo(*args, **kwargs):
print(args)
print(kwargs)
gl_nums = (1, 2, 3)
gl_dict = {"name": "小明", "age": 18}
demo(gl_nums, gl_dict)
输出:
((1, 2, 3), {‘name’: ‘小明’, ‘age’: 18})
{}