*args 和 **kwargs 的使用 [英] Use of *args and **kwargs

查看:51
本文介绍了*args 和 **kwargs 的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对 *args**kwargs 的概念有困难.

到目前为止,我已经了解到:

  • *args = 参数列表 - 作为位置参数
  • **kwargs = 字典 - 其键成为单独的关键字参数,值成为这些参数的值.

我不明白这对什么编程任务会有帮助.

也许:

我想输入列表和字典作为函数的参数,同时作为通配符输入,这样我就可以传递任何参数?

有没有一个简单的例子来解释如何使用*args**kwargs?

我发现的教程也只使用了*"和一个变量名.

*args**kwargs 只是占位符还是完全使用 *args**kwargs> 在代码中?

解决方案

语法是***.名称 *args**kwargs 只是约定俗成的,但没有硬性要求使用它们.

当您不确定可以将多少参数传递给您的函数时,您可以使用 *args,即它允许您将任意数量的参数传递给您的函数.例如:

<预><代码>>>>def print_everything(*args):对于计数,枚举(args)中的内容:...打印('{0}.{1}'.format(计数,事物))...>>>打印_一切('苹果','香蕉','卷心菜')0. 苹果1.香蕉2.白菜

同样,**kwargs 允许你处理你没有预先定义的命名参数:

<预><代码>>>>def table_things(**kwargs):...对于名称,kwargs.items() 中的值:...打印('{0} = {1}'.format(名称,值))...>>>table_things(苹果 = '水果',白菜 = '蔬菜')卷心菜=蔬菜苹果 = 水果

您也可以将这些与命名参数一起使用.显式参数首先获取值,然后将其他所有内容传递给 *args**kwargs.命名参数首先出现在列表中.例如:

def table_things(titlestring, **kwargs)

您也可以在同一个函数定义中同时使用两者,但 *args 必须出现在 **kwargs 之前.

您还可以在调用函数时使用 *** 语法.例如:

<预><代码>>>>def print_three_things(a, b, c):...打印('a = {0},b = {1},c = {2}'.格式(a,b,c))...>>>mylist = ['土豚','狒狒','猫']>>>print_three_things(*mylist)a = 土豚,b = 狒狒,c = 猫

正如您在本例中所看到的,它获取项目的列表(或元组)并将其解包.通过这种方式,它将它们与函数中的参数相匹配.当然,您可以在函数定义和函数调用中都使用 *.

So I have difficulty with the concept of *args and **kwargs.

So far I have learned that:

  • *args = list of arguments - as positional arguments
  • **kwargs = dictionary - whose keys become separate keyword arguments and the values become values of these arguments.

I don't understand what programming task this would be helpful for.

Maybe:

I think to enter lists and dictionaries as arguments of a function AND at the same time as a wildcard, so I can pass ANY argument?

Is there a simple example to explain how *args and **kwargs are used?

Also the tutorial I found used just the "*" and a variable name.

Are *args and **kwargs just placeholders or do you use exactly *args and **kwargs in the code?

解决方案

The syntax is the * and **. The names *args and **kwargs are only by convention but there's no hard requirement to use them.

You would use *args when you're not sure how many arguments might be passed to your function, i.e. it allows you pass an arbitrary number of arguments to your function. For example:

>>> def print_everything(*args):
        for count, thing in enumerate(args):
...         print( '{0}. {1}'.format(count, thing))
...
>>> print_everything('apple', 'banana', 'cabbage')
0. apple
1. banana
2. cabbage

Similarly, **kwargs allows you to handle named arguments that you have not defined in advance:

>>> def table_things(**kwargs):
...     for name, value in kwargs.items():
...         print( '{0} = {1}'.format(name, value))
...
>>> table_things(apple = 'fruit', cabbage = 'vegetable')
cabbage = vegetable
apple = fruit

You can use these along with named arguments too. The explicit arguments get values first and then everything else is passed to *args and **kwargs. The named arguments come first in the list. For example:

def table_things(titlestring, **kwargs)

You can also use both in the same function definition but *args must occur before **kwargs.

You can also use the * and ** syntax when calling a function. For example:

>>> def print_three_things(a, b, c):
...     print( 'a = {0}, b = {1}, c = {2}'.format(a,b,c))
...
>>> mylist = ['aardvark', 'baboon', 'cat']
>>> print_three_things(*mylist)
a = aardvark, b = baboon, c = cat

As you can see in this case it takes the list (or tuple) of items and unpacks it. By this it matches them to the arguments in the function. Of course, you could have a * both in the function definition and in the function call.

这篇关于*args 和 **kwargs 的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆