python 2.7,具有位置,关键字参数以及可变数量的参数(任意参数列表)的函数

python 2.7, functions with positional, keyword arguments as well as variable number of arguments (arbitrary argument lists)

是否有一种方法来实现具有可变参数数(*args)和非可选关键字参数的函数?我想实现这样的功能:

1
2
3
4
5
def foo(positional arguments, keyword arguments,*args):

     {
      ...
     }

然而,现在似乎没有办法正确调用这个函数。让arg1和arg2成为我们要在函数调用中传递的(*arg)类型的参数。

1
foo(posargs, kwarg=value, arg1, arg2)

不允许(Kwarg必须在最后),同时:

1
foo(posargs,arg1,arg2,kwarg=value)

会导致错误的函数调用,因为arg1按位置分配给了kwarg,因为在函数调用中提供了关键字值。

是否有任何替代的定义或调用语法可以避免这种情况(除了在函数调用中使用作为位置参数的Kwarg值进行调用的简单解决方案之外)?


(P)This is a Python 3 Feature,introduced in PEP 3102.(Python,Default Keyword Arguments after variable length positional arguments)(p)(P)In Python 2,you will have to extract the keyword arguments from EDOCX1(p)字母名称


(P)*Args will collect the unmatched positional arguments into a tuple.The**Kwargs works to collect unmatched keyword arguments and puts them not into a tuple but into a dictionary.(p)(P)From O'Reilly's Learning Python page 535:(p)字母名称(P)From page 531 of the same book:"In both the call and header,the**args form must appear last if present."如果你在任何其他命令中争论,你会得到一个系统错误,因为组合可能是模糊的。"(p)