python星号解包

a = (1, 2, 3, 4, 5, 6)
b = [2, 3, 4, 5, 5, 6]
c = {"name": 'zhang', "age": 2}

print(*a)
print(*b)
print(*c

结果为

1 2 3 4 5 6
2 3 4 5 5 6
name age
def f(str1, *args, **kwargs):
    print(str1, args, kwargs)

l = [1, 2, 3]
t = [4, 5, 6]
d = {"a":7, "b":8, "c":9}

f(1, 2)
f(1, 2, 3, "python")
f("python", a=1, b=2, c=3)

print("================")

f("python", l, d)
f("python", *t)
f("python", *l, **d)
f("python", q="winning", **d)


运行结果:
1 (2,) {}
1 (2, 3, 'python') {}
python () {'a': 1, 'b': 2, 'c': 3}
================
python ([1, 2, 3], {'a': 7, 'b': 8, 'c': 9}) {}
python (4, 5, 6) {}
python (1, 2, 3) {'a': 7, 'b': 8, 'c': 9}
python () {'q': 'winning', 'a': 7, 'b': 8, 'c': 9}

推荐阅读更多精彩内容