python *args **kargs 详解

  1. *args代表该参数位置可以放置任意个数的数据,最终都会转化成元祖数据类型在函数体内处理

  2. **kargs代表该参数位置可以放置k=v格式的数据,最终都会转化成字典类型在函数体内处理

def foo_single_arg(arg):
    print("arg = ", arg)

def foo_multi_args(arg, *args):
    print("arg = ", arg)
    print("*args = ", args)

def foo_multi_args_with_key(arg, *args, **kargs):
    print("arg = ", arg)
    print("*args = ", args)
    print("**kargs = ", kargs)
>> foo_single_arg(2)
>> arg = 2

>> foo_multi_args(2, "hello", "world", 3, 4)
>> arg = 2
>> *args = ("hello", "world", 3, 4)

>> foo_multi_args_with_key(2, "hello", "world", 3, 4)
>> arg = 2
>> *args = ("hello", "world", 3, 4)
>> **kargs = {}

>> foo_multi_args_with_key(2, "hello", "world", 3, 4, name="xjj")
>> arg = 2
>> *args = ("hello", "world", 3, 4)
>> **kargs = {'name':'xjj'}
posted @ 2022-02-17 16:08  27岁的太阳  阅读(340)  评论(0编辑  收藏  举报