使用*和**解包(Unpacking)函数的实参

1.解包序列字面量(*)

//字符串
def f(a, b):
    print(a, b)

c = "12"
f(*c)  # 1 2
//列表
def f(a, b):
    print(a, b)

c = [1, 2]
f(*c)  # 1 2
//元组
def f(a, b):
    print(a, b)

c = (1, 2)
f(*c)  # 1 2
//范围
def f(a, b):
    print(a, b)

c = range(1, 3)
f(*c)  # 1 2

2.解包字典字面量(**)

def f(a, b):
    print(a, b)

c = {"a": 1, "b": 2}
f(**c)  # 1 2

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/experience/pythonexp/36664.html

huoxiaoqiang的头像huoxiaoqiang

发表回复

登录后才能评论
分享本页
返回顶部