Python中的**kwargs和**

**kwargs

def fun(a, **kwargs):
    print("a is", a)
    print("We expect kwargs 'b' and 'c' in this function")
    print("b is", kwargs['b'])
    print("c is", kwargs['c'])
fun(1, b=3, c=5)

加上了**kwargs则可以接收其他任意数量的变量

**


def fun(a, b, c):
    print a, b, c

d={
    
    'c':3}
fun(1,2,**d)   # ---注意不能超过数量

d={
    
    'a':7,'b':8,'c':9}
fun(**d)

猜你喜欢

转载自blog.csdn.net/caihuanqia/article/details/112907624
今日推荐