python 中函数参数使用**zidian 和zidian=zidian的区别

python 中函数参数使用**zidian 和zidian=zidian的区别:

我写了四中情况分别比较了输出结果:

1:

zidian={
    'name':'xingxing','age':23
}
def xing(zidian=zidian):
    for k,v in zidian.items():
        print(k,v)

print(xing(**zidian))

输出报错:TypeError: xing() got an unexpected keyword argument 'name'

2:

zidian={
    'name':'xingxing','age':23
}
def xing(zidian=zidian):
    for k,v in zidian.items():
        print(k,v)
print(xing(zidian=zidian))
输出:name xingxing
     age 23
     None
3:
zidian={
    'name':'xingxing','age':23
}
def xing(**zidian):
    for k,v in zidian.items():
        print(k,v)
print(xing(**zidian))
输出:name xingxing
      age 23
      None
4:
zidian={
    'name':'xingxing','age':23
}
def xing(**zidian):
    for k,v in zidian.items():
        print(k,v)
print(xing(zidian=zidian))
输出:zidian {'name': 'xingxing', 'age': 23}
      None
结论:**zidian作为参数意思是接收任意多的默认参数幷将其装载为字典幷进行遍历幷输出,3和4函数都是这样,只不过3函数调用

      时相当于将一个字典拆开传递进去,而4函数是将一个字典作为一个默认参数传递给函数,相当于装载完成是只有一个内容的字典,1和2参数为zidian=zidian时,相当于传递的参数就是字典类型。

你可能感兴趣的:(python 中函数参数使用**zidian 和zidian=zidian的区别)