1. 程式人生 > >python 中* 和**的作用

python 中* 和**的作用

先舉個 ** 使用的例子:

data = {"a": 1, "b": 2}


def foo(**kwargs):
	print kwargs


foo(a=1, b=2)    
# foo(data)  # 這種呼叫data的方法會報錯
>>>
{'a': 1, 'b': 2}

 可以看出,**代表了給函式傳入引數的方式是:a=1形式,即:引數名=引數值,並且不管傳入幾個值,該函式都會轉化為字典處理

 

再舉一個*的使用的例子:

data = {"a": 1, "b": 2}


def foo(*kwargs):
	print kwargs


foo(data, "aaa", "bbb")

 可以看出,使用*args可以傳入多個引數,並且處理時,按照現將多個值轉成一個元組處理