Python变量前‘*‘和‘**‘的作用
在Python的在形参前加'*'和'**'表示动态形参
在形参前加'*'表示可以接受多个实参值存进数组
def F(a, *b)print(a)print(b)F(1, 2, 3)'''
(2, 3)
'''
对于在形参前加'**'表示表示接受参数转化为字典类型
def F(**a)print(a)F(x=1, y=2)#{'x': 1, 'y': 2}
混合运用
def F(a, *b, **c)print(a)print(b)print(c)F(1, 2, 3, x=4, y=5)'''
(2, 3)
{'x': 4, 'y': 5}
'''
def F(a, *b, **c)print(a)print(b)print(c)F(1, 2, 3, x=4, y=5)'''
(2, 3)
{'x': 4, 'y': 5}
'''
def F(**a)print(a)dt = dict(x=1, y=2)
F(x=1, y=2)
F(**dt) #作为字典传入'''
{'x': 1, 'y':2}
{'x': 1, 'y':2}
函数调用时
dt = dict(color='red', fontproperties='SimHei')
plt.plot(**dt)
等价于
plt.plot(color='red', fontproperties='SimHei')
'''
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
