Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So I have difficulty with the concept of *args and **kwargs . (所以我很难理解*args**kwargs的概念。)

So far I have learned that: (到目前为止,我已经了解到:)

  • *args = list of arguments - as positional arguments (*args =参数列表-作为位置参数)
  • **kwargs = dictionary - whose keys become separate keyword arguments and the values become values of these arguments. (**kwargs =字典-其键成为单独的关键字参数,而值则成为这些参数的值。)

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? (我认为要输入列表和字典作为函数的参数,并与通配符同时输入,因此我可以传递ANY参数吗?)

Is there a simple example to explain how *args and **kwargs are used? (有一个简单的示例来说明如何使用*args**kwargs吗?)

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? (*args**kwargs只是占位符还是在代码中使用了*args**kwargs ?)

  ask by MacPython translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
175 views
Welcome To Ask or Share your Answers For Others

1 Answer

The syntax is the * and ** . (语法是*** 。) The names *args and **kwargs are only by convention but there's no hard requirement to use them. (名称*args**kwargs仅是约定俗成的,但使用它们并没有硬性要求。)

You would use *args when you're not sure how many arguments might be passed to your function, ie it allows you pass an arbitrary number of arguments to your function. (当您不确定可以向函数传递多少个参数时,可以使用*args ,即,它允许您将任意数量的参数传递给函数。) 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: (同样, **kwargs允许您处理尚未预先定义的命名参数:)

>>> 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 . (显式参数首先获取值,然后将其他所有*args传递给*args**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 . (您也可以在相同的函数定义中使用两者,但是*args必须在**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. (当然,在函数定义和函数调用中都可以带有* 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...