首页 文章

使用* args和** kwargs [重复]

提问于
浏览
1246

这个问题在这里已有答案:

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

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

  • *args =参数列表 - 作为位置参数

  • **kwargs = dictionary - 其键成为单独的关键字参数,值成为这些参数的值 .

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

也许:

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

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

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

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

11 回答

  • 454

    这是一个使用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}
    
  • 15

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

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

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

  • 9

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

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

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

    >>> multiply(1,2,3)
    6
    
    or
    
    >>> numbers = [1,2,3]
    >>> multiply(*numbers)
    6
    
  • 17
    • 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
    
  • 1524

    名称 *args**kwargs**kw 纯粹是按惯例 . 它使我们更容易阅读彼此的代码

    一个方便的地方是使用struct模块

    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改变并且元组的大小发生变化,我将不得不返回并编辑那个真正的长行

  • 7

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

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

    myfunction(myarg=1)
    
  • 40
    • 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 .

  • 67

    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
    

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

  • 35

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

    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
    

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

  • 3

    使用 *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类 .

  • 290

    请注意,* 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
    

相关问题