如何在 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
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP