使用* args和** kwargs

时间:2010-08-03 08:28:08

标签: python args kwargs

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

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

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

我不明白这会对哪些编程任务有所帮助。

也许:

我认为输入列表和字典作为函数AND的参数同时作为通配符,所以我可以传递任何参数?

是否有一个简单的示例来解释如何使用*args**kwargs

我发现的教程也只使用了“*”和变量名。

*args**kwargs只是占位符,还是在代码中使用了*args**kwargs

11 个答案:

答案 0 :(得分:1579)

The syntax is the * and **。名称*args**kwargs仅限于惯例,但没有必要使用它们。

如果您不确定可以向您的函数传递多少个参数,则可以使用*args,即它允许您向函数传递任意数量的参数。例如:

>>> 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

类似地,**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

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

def table_things(titlestring, **kwargs)

您也可以在同一个函数定义中使用它们,但*args必须在**kwargs之前出现。

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

>>> 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

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

答案 1 :(得分:464)

使用*args**kwargs非常有用的一个地方是子类化。

class Foo(object):
    def __init__(self, value1, value2):
        # do something with the values
        print value1, value2

class MyFoo(Foo):
    def __init__(self, *args, **kwargs):
        # do something else, don't care about the args
        print 'myfoo'
        super(MyFoo, self).__init__(*args, **kwargs)

这样你可以扩展Foo类的行为,而不必过多地了解Foo。如果您正在编程可能会更改的API,这可能非常方便。 MyFoo只是将所有参数传递给Foo类。

答案 2 :(得分:301)

以下是使用3种不同类型参数的示例。

def func(required_arg, *args, **kwargs):
    # required_arg is a positional-only parameter.
    print required_arg

    # args is a tuple of positional arguments,
    # because the parameter name has * prepended.
    if args: # If args is not empty.
        print args

    # kwargs is a dictionary of keyword arguments,
    # because the parameter name has ** prepended.
    if kwargs: # If kwargs is not empty.
        print kwargs

>>> func()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func() takes at least 1 argument (0 given)

>>> func("required argument")
required argument

>>> func("required argument", 1, 2, '3')
required argument
(1, 2, '3')

>>> func("required argument", 1, 2, '3', keyword1=4, keyword2="foo")
required argument
(1, 2, '3')
{'keyword2': 'foo', 'keyword1': 4}

答案 3 :(得分:69)

这是我最喜欢使用**语法的地方之一,如Dave Webb的最后一个例子:

mynum = 1000
mystr = 'Hello World!'
print "{mystr} New-style formatting is {mynum}x more fun!".format(**locals())

与仅使用名称本身相比,我不确定它是否非常快,但输入起来要容易得多!

答案 4 :(得分:43)

* args和** kwargs有用的一种情况是编写包装器函数(例如装饰器)时需要能够接受任意参数传递给被包装的函数。例如,一个简单的装饰器打印参数并返回被包装函数的值:

def mydecorator( f ):
   @functools.wraps( f )
   def wrapper( *args, **kwargs ):
      print "Calling f", args, kwargs
      v = f( *args, **kwargs )
      print "f returned", v
      return v
   return wrapper

答案 5 :(得分:35)

* args和** kwargs是Python的特殊魔术功能。 想想一个可能有未知数量的参数的函数。例如,无论出于何种原因,您希望具有对未知数量的数字求和的函数(并且您不想使用内置求和函数)。所以你写这个函数:

def sumFunction(*args):
  result = 0
  for x in args:
    result += x
  return result

并使用它:sumFunction(3,4,6,3,6,8,9)。

** kwargs有不同的功能。使用** kwargs,您可以为函数提供任意关键字参数,并且可以作为dictonary访问它们。

def someFunction(**kwargs):
  if 'text' in kwargs:
    print kwargs['text']

调用someFunction(text =“foo”)将打印foo。

答案 6 :(得分:18)

想象一下你有一个功能,但你不想限制它所需的参数数量。 例如:

>>> import operator
>>> def multiply(*args):
...  return reduce(operator.mul, args)

然后你使用这个函数,如:

>>> multiply(1,2,3)
6

or

>>> numbers = [1,2,3]
>>> multiply(*numbers)
6

答案 7 :(得分:16)

名称*args**kwargs**kw纯属惯例。它使我们更容易阅读彼此的代码

一个方便的地方是使用struct module

struct.unpack()返回一个元组,而struct.pack()使用可变数量的参数。在操作数据时,能够将元组传递给struck.pack()例如

是很方便的
tuple_of_data = struct.unpack(format_str, data)
... manipulate the data
new_data = struct.pack(format_str, *tuple_of_data)
如果没有这种能力,你将被迫写作

new_data = struct.pack(format_str, tuple_of_data[0], tuple_of_data[1], tuple_of_data[2],...)

这也意味着如果format_str改变并且元组的大小发生变化,我将不得不返回并编辑那个真正的长行

答案 8 :(得分:9)

请注意,* args / ** kwargs是函数调用语法的一部分,而不是真正的运算符。这有一个特殊的副作用,我遇到了,你不能使用* args扩展与print语句,因为print不是一个函数。

这似乎是合理的:

def myprint(*args):
    print *args

不幸的是它没有编译(语法错误)。

编译:

def myprint(*args):
    print args

但是将参数打印为元组,这不是我们想要的。

这是我解决的解决方案:

def myprint(*args):
    for arg in args:
        print arg,
    print

答案 9 :(得分:7)

这些参数通常用于代理函数,因此代理可以将任何输入参数传递给目标函数。

def foo(bar=2, baz=5):
    print bar, baz

def proxy(x, *args, **kwargs): # reqire parameter x and accept any number of additional arguments
    print x
    foo(*args, **kwargs) # applies the "non-x" parameter to foo

proxy(23, 5, baz='foo') # calls foo with bar=5 and baz=foo
proxy(6)# calls foo with its default arguments
proxy(7, bar='asdas') # calls foo with bar='asdas' and leave baz default argument

但由于这些参数会隐藏实际的参数名称,因此最好避免使用它们。

答案 10 :(得分:3)

您可以查看python文档(FAQ中的docs.python.org),但更具体地说是一个很好的解释the mysterious miss args and mister kwargs (courtesy of archive.org)(原始的,死链接是here)。

简而言之,当使用函数或方法的可选参数时,都会使用它们。 正如Dave所说,当你不知道可以传递多少个参数时会使用* args,而当你想要处理由name和value指定的参数时会使用** kwargs:

myfunction(myarg=1)
相关问题