python *args **kargs 详解
-
*args代表该参数位置可以放置任意个数的数据,最终都会转化成元祖数据类型在函数体内处理
-
**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'}
本文来自博客园,作者:27岁的太阳,转载请注明原文链接:https://www.cnblogs.com/isxjj/p/15905100.html