unpack参数列表

如果想把一个列表的内容当作参数传进函数里,可以使用unpack

如下例

In [1]: range(3,6)
Out[1]: [3, 4, 5]

In [2]: args = [3, 6]

In [3]: range(*args)
Out[3]: [3, 4, 5]

unpack列表只需要前面加*就行

也可以unpack字典成关键字参数,前面加**就行

In [1]: def server_address(host='0.0.0.0', port=80):
   ...:     print 'host:',host,'\nport','port'
   ...:

In [2]: d = {'host':'127.0.0.1', 'port':80}

In [3]: server_address(**d)
host: 127.0.0.1
port port

results matching ""

    No results matching ""