def f1(a,b,c=0,*args,**kw): print('a=',a,'b=',b,'c=',c,'args=',args,'kw=',kw) def f2 (a,b,c=0,*,d,**kw): print('a=',a,"b=",b,'c=',c,d,'kw=',kw) f1("zs","hsdf",c=12) a= zs b= hsdf c= 12 args= () kw= {} f1("zs","hsdf",3,(1,2,3),kw='jac') a= zs b= hsdf c= 3 args= ((1, 2, 3),) kw= {'kw': 'jac'} f1("zs","hsdf",3,"dsf","dsf",kw='jac') a= zs b= hsdf c= 3 args= ('dsf', 'dsf') kw= {'kw': 'jac'} f1("zs","hsdf",3,"dsf","dsf","dsffd",kw='jac') a= zs b= hsdf c= 3 args= ('dsf', 'dsf', 'dsffd') kw= {'kw': 'jac'} f1("zs","hsdf",3,"dsf","dsf","dsffd",kw='jac',ke='df') a= zs b= hsdf c= 3 args= ('dsf', 'dsf', 'dsffd') kw= {'kw': 'jac', 'ke': 'df'} f1("zs","hsdf",3,"dsf","dsf","dsffd",k='jac',ke='df',fc='kk') a= zs b= hsdf c= 3 args= ('dsf', 'dsf', 'dsffd') kw= {'k': 'jac', 'fc': 'kk', 'ke': 'df'}
f2("df","sdf",d=4,sdf="sdf",dsj=[1,2],jfj=3,sdjfh=(12,3)) a= df b= sdf c= 0 4 kw= {'sdf': 'sdf', 'dsj': [1, 2], 'jfj': 3, 'sdjfh': (12, 3)}
args=(1,2,34) kw={'d':99,'x':'#'} f1(*args,**kw) a= 1 b= 2 c= 3 args= (4,) kw= {'d': 99, 'x': '#'}
这里可以理解*为可变参数,**kw为关键字参数,可变参数就是数量可变,所以可以用元组传入,关键字参数因为有名字和对应的数值两部分,正好就是dict,key-value对应
def h(*y): he=1 for i in y: he=i*he return (he)
计算多个数乘,原本he写在外边,结果报错,好像在外边,他就不认识了,明白了,写在外面他就不是不这个函数里面的,也就是没办法用。