1、* 操作符後面跟的是元組或列表,**後面跟的是字典
2、調用函數時,* 或 ** 用於將元組或字典拆分並作爲獨立的函數參數傳遞
*tuple_arg 將 [1, 2] 拆解爲獨立的位置參數 1,2,**kw_arg將 {‘c’:5, ‘d’:6} 拆解爲獨立的關鍵字參數傳遞,test_args(*tuple_arg, **kw_arg) 等價於 test_args(1, 2, c=5, d=6)
def test_args(a,b,c,d):
print a,b,c,d # output:1 2 5 6
tuple_arg = [1,2]
kw_arg = {'c':5, 'd':6}
test_args(*tuple_arg, **kw_arg)
3、定義函數時,* 或 ** 用於壓縮被傳遞到函數中的參數到元組或字典中,用於參數個數未知的情況
從輸出可以看到,參數1,2被壓縮到了元組tp中,c=5,d=6被壓縮到了字典kv中
def test_args(*tp, **kv):
print 'tp is ', tp
print 'kv is ', kv
test_args(1, 2, c=5, d=6)
# output:
# tp is (1, 2)
# kv is {'c': 5, 'd': 6}
3、* 或 ** 還有很多其他用法,有的用法只在python 3中支持,具體見 python中星號的本質及其使用方法