python *tuple和**dict的含义

原文:https://stackoverflow.com/questions/21809112/what-does-tuple-and-dict-mean-in-python

*tuple可以理解为把tuple的内容展开

def foo(x, y):
    print(x, y)

>>> t = (1, 2)
>>> foo(*t)
1 2

**dict可以理解为以key=value的方式展开

def foo(x, y):
    print(x, y)

>>> d = {'x':1, 'y':2}
>>> foo(**d)
1 2
posted @ 2024-09-28 14:09  寻找繁星  阅读(1)  评论(0编辑  收藏  举报