函数的参数数量不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值。
这样说吧:这个是Python函数可变参数args及kwargs *args表示任何多个无名参数,它是一个tuple **kwargs表示关键字参数,它是一个dict
示例:
def highschool_class(number,*args):
print("the highschool_class has %s "%number) #高中一个班级的人数
print(*args)
return (args)
highschool_class(59,30,29)
结果:
the highschool_class has 59
30 29
Process finished with exit code 0
def highschool_class(number,*args,**kwargs): print(“the highschool_class has %s “%number) #高中一个班级的人数 print(args)
print(kwargs)
return (args,kwargs)
highschool_class(59,30,29,male = ’30’,female = ’29’)
结果:
the highschool_class has 59
30 29
{‘male’: ’30’, ‘female’: ’29’}
Process finished with exit code 0