使用*和**解包(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

(0)
huoxiaoqiang的头像huoxiaoqiang
上一篇 1天前
下一篇 1天前

相关推荐

发表回复

登录后才能评论