Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 语法错误:关键字arg之后是非关键字arg_Python_Python 2.7_Function - Fatal编程技术网

Python 语法错误:关键字arg之后是非关键字arg

Python 语法错误:关键字arg之后是非关键字arg,python,python-2.7,function,Python,Python 2.7,Function,我正在spyder中使用Python 2.7.13 def test(a,b,c='c',*args): for item in args: print item 此函数定义在Python 2.7中有效,但当我尝试传入args时,它会在关键字arg错误后给出非关键字arg: 给出了: non-keyword arg after keyword arg 但这是: test(1,2,3,4,5) 他正在工作 我不确定这里有什么问题,因为在c='c'b之前放置了*args

我正在spyder中使用Python 2.7.13

def test(a,b,c='c',*args):
    for item in args:
        print item
此函数定义在Python 2.7中有效,但当我尝试传入args时,它会在关键字arg错误后给出非关键字arg:

给出了:

non-keyword arg after keyword arg
但这是:

test(1,2,3,4,5)
他正在工作

我不确定这里有什么问题,因为在
c='c'
b之前放置了
*args
b

def test(a,b,*args,c='c'):
    for item in args:
        print item
这在函数定义中给了我一个错误

上述代码只是一个虚拟示例,原始代码如下所示:

def export_plot_as_mat(fname, undersamp, undersamp_type, n_comp, b_s, n_iter, fit_alg, transf_alg, alpha_train, alpha_test, export_info=False, *args):

    info = ('undersampling=' + str(undersamp) + ' undersampling_type=' +str(undersamp_type) + 
        ' n_comp=' + str(n_comp) + ' batch_size=' + str(b_s) + 
        ' n_iter=' + str(n_iter) + ' fit_alg=' + str(fit_alg) + 
        ' transform_alg=' + str(transf_alg) + ' alpha_train=' + 
        str(alpha_train) + ' alpha_test=' + str(alpha_test))

    d = [(str(args[i]), args[i]) for i in range(len(args))]

    if export_info:
        d.append('info',info)
    sp.io.savemat(fname + '.mat', d)
我希望可以选择导出用于生成我正在导出的数据的参数。

这里有几件事

将test()中的
c='c'
更改为仅
'c'

你也可以把你最后的to参数放在一个列表中,把它们打印在同一行。或者让它们保持原样,以单独的行打印它们

def test(a,b,c='c',*args):
    for item in args:
        print item

test(1,2,'c',[10,11])
结果:

[10, 11]
对于参数
c='c'
,您可以在那里传递任何需要的内容,并与
c
进行交互:

举个例子:

def test(a,b,c='c',*args):
    print c
    for item in args:
        print item


test(1,2,'g',10,11)
结果:

g
10
11

这是Python2.x如何进行参数解析的一个有趣的附带案例。大致上,Python有一些逻辑,用于如何将传递给函数的参数与它所期望的参数“匹配”,而这种逻辑在Python 3中发生了更改


获取所需内容的最简单方法是接受
**kwargs
而不是指定
c=0
,并手动执行默认参数。

定义很好,因为可以按位置指定
c
。电话是问题所在;在另一个位置参数之前指定了关键字参数


Python2.x无法定义只包含关键字的参数。

更改顺序:
test(1,2,10,11,c='c')
。Python将做正确的事情,并在
*args
@Boldewyn中收集所有非关键字参数。这在Python 3.x中可能有效,但在Python 2.7中,这给了我一个错误
test()关键字参数“c”有多个值。
@PedrovonHertwig我希望参数有不同的大小,所以我不传递常规参数。重复的参数更适合Python 3解决方案,然而OP使用的是Python 2.7。@Aryamcarthy因为在这种情况下定义的顺序是正确的,所以它不是同一个问题。我想将“c”指定为c='c',看看分配给“c”的是什么。你能更清楚一点吗?要查看分配的
'c'
是什么意思。可能使用的示例在
=
的两侧都不是
c
,因此我可以理解您需要什么。在更新的问题中,参数是
export\u info=False
,因此在函数调用中可以看到信息是否也应该导出,还是仅导出参数。仅仅传递
True
False
是不可读的,根据这一点,我认为在Python2.7中这是不可能的。我不确定2.7是否有办法解决这个问题,但在Python3中可能是可行的。我认为这是一个解决方案,但最终还是有点难看。这个定义与上面提到的函数调用本身存在矛盾,将*args放在关键字之前会在函数定义中引发错误是的,这是第二个错误。它并没有改变第一个定义是正确的这一事实。无论在Python2.x中如何定义
test
,像
test(1,2,c=3,4,5)
这样的调用都是非法的;您不能在关键字参数后指定位置参数。因此,解决此问题的方法是在
**kwargs
参数中定义
c
,并使用
kwargs[0]
调用它,不知何故,我认为它可能会以问题的方式工作,因为定义是正确的。定义是正确的,因为Python2没有明确区分“可选位置参数”和“关键字参数”。如果定义
def测试(a、b、*args、**kwargs)
,则
c
只能指定为关键字参数。(这与我之前所说的Python2没有只包含关键字的参数略有不同,因为在本例中,您并没有显式声明
c
;您只是声称
test
可以接受任意关键字参数,但您并没有保证它们都会被确认。)
g
10
11