Python 如何使用*args和**kwargs

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中的gadgets_price.py命令,我们可以运行程序并获得如下所示的输出 –

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程