如何在Python中使用*args和**kwargs?


在這篇文章中,我們將討論*args和**kwargs屬性及其在Python中的用法。

為了向函式傳遞不確定數量的引數,通常使用*args和**kwargs屬性。*args屬性用於向函式呼叫傳送非關鍵字可變長度引數,而**kwargs關鍵字用於向函式傳遞關鍵字可變長度引數。

  • 關鍵字引數(或引數)是在函式呼叫中由識別符號“=”(例如“name=”)引導的值。

  • 使用*args和**kwargs的主要好處是可讀性和便捷性,但應謹慎使用。

注意 − 不必一定要寫*args和**kwargs。只需要星號(*)即可。例如,你也可以寫*var和**vars。

在Python中使用*args

要在函式中傳遞可變數量的非關鍵字引數,需要在引數名稱前使用星號(*)。

示例

以下是一個使用*args在函式中傳遞引數的示例:

def multiply (*args):
   print(args, type(args))
multiply(6,78)

輸出

因此,我們可以確信,接收這些提供引數的方法會建立一個元組,其名稱與引數名稱相同,只是去掉了*。

(6, 78) <class 'tuple'>

示例

現在讓我們將可變數量的引數乘以我們的multiply()函式,如下例所示:

def multiply(*args):
   x = 1
   for i in args:
      x = x * i
   print ('The product of the numbers is:',x)
multiply(5,87)
multiply(8,11)
multiply(6,9,4,7)
multiply(2,11,98)

輸出

以下是上述程式碼的輸出:

The product of the numbers is: 435
The product of the numbers is: 88
The product of the numbers is: 1512
The product of the numbers is: 2156

注意 − 引數名稱不必是args;它可以是任何名稱。在本例中是numbers。但是,使用*args作為名稱是普遍接受的。

在Python中使用**kwargs

我們可以使用**kwargs為Python函式提供任意數量的關鍵字引數。

要在函式中指示這種型別的引數,我們在引數名稱前使用雙星號(**),這表示存在一個具有可變長度的關鍵字引數字典。

示例

以下是一個使用**kwargs在函式中傳遞引數的示例:

def gadgets_price(**kwargs):
   print(kwargs,type(kwargs))
gadgets_price(laptop= 60000, smartphone=10000, earphones =500)

輸出

引數作為字典傳遞,並在函式內部建立一個與引數名稱相同的字典,只是去掉了**,正如我們所看到的。

{'laptop': 60000, 'smartphone': 10000, 'earphones': 500} <class 'dict'>

示例

現在讓我們完成gadgets_price()函式,以返回所有小工具的總金額,如下例所示:

def gadgets_price(**items):
   total = 0
   for price in items.values():
      total += price
   return total
print('The net amount of the gadgets is:',(gadgets_price(laptop= 60000, smartphone=10000, earphones =500)))
print('The net amount of the gadgets is:',(gadgets_price(laptop= 60000, smartphone=10000, desktop =45768)))
print('The net amount of the gadgets is:',(gadgets_price(laptop= 60000, TV=85965)))

輸出

以下是上述程式碼的輸出:

The net amount of the gadgets is: 70500
The net amount of the gadgets is: 115768
The net amount of the gadgets is: 145965

注意 − 引數名稱不必是kwargs;它可以是任何名稱。在本例中是items。但是,使用**kwargs作為名稱是普遍接受的。

在函式呼叫中使用*args和**kwargs

我們可以使用*args和**kwargs將引數傳遞給函式。

示例

函式中標識了三個引數:‘laptop’,‘smartphone’和‘earphone’。函式將打印出每一個引數。然後,使用星號語法,我們可以向函式提供一個設定為可迭代物件(在本例中為元組)的變數,如下例所示:

def gadgets(laptop, smartphone, earphone):
   print('The first gadget is of:',laptop)
   print('The second gadget is of:',smartphone)
   print('The third gadget is of:',earphone)
args = ('Lenovo','Samsung','JBL')
gadgets(*args)

輸出

使用python gadgets.py命令,我們可以執行程式並獲得如下所示的輸出:

The first gadget is of: Lenovo
The second gadget is of: Samsung
The third gadget is of: JBL

注意 - 以上程式也可以更改為使用可迭代列表資料型別和替代變數名。讓我們也結合命名引數和*args語法

def gadgets_price(laptop, smartphone, earphone):
   print('The first gadget is of price:',laptop)
   print('The second gadget is of price:',smartphone)
   print('The third gadget is of price:',earphone)
list = [15438,499]
gadgets_price(54000,*list)

以上程式碼的輸出如下:

The first gadget is of price: 54000
The second gadget is of price: 15438
The third gadget is of price: 499

示例

類似地,你可以使用關鍵字**kwargs引數呼叫函式。我們將建立一個名為kwargs的變數,它將表示一個包含三個鍵值對的字典,但你可以隨意命名它,並將其提供給一個包含三個引數的函式,如下所示:

def gadgets_price(laptop, smartphone, earphone):
   print('The first gadget is of price:',laptop)
   print('The second gadget is of price:',smartphone)
   print('The third gadget is of price:',earphone)
kwargs = {'laptop':54678, 'smartphone':15893, 'earphone':499}
gadgets_price(**kwargs)

輸出

使用python some_gadgets_price.py命令,我們可以執行程式並獲得如下所示的輸出:

The first gadget is of price: 54678
The second gadget is of price: 15893
The third gadget is of price: 499

更新於:2022年12月19日

2K+ 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.