繁体   English   中英

SyntaxError:位置参数跟随关键字参数

[英]SyntaxError: positional argument follows keyword argument

我想知道下面的说法是否正确。

SyntaxError:位置参数跟随关键字参数

我知道什么是位置参数和关键字参数,但对上述语句感到困惑。 例如当我们说

A 跟随 B 这意味着

//这意味着“A”在“B”之后

因此,以同样的方式,当我们调用任何 function 时,我们应该先传递位置参数,然后传递关键字参数。 在这种情况下,正确的陈述应该是

***SyntaxError: keyword argument follows positional argument***

例子:

    def test(a,b,c):
        print(f"Sum of all no is : {a+b+c}")
    test(a=10,20,c=30)
    test(a=10,20,c=30)
//output                   ^
SyntaxError: positional argument follows keyword argument

示例2:

Passing positional argument first.
    def test(a,b,c):
        print(f"Sum of all no is : {a+b+c}")
    test(20,c=10,b=30)
    //output
    Sum of all no is : 60

这是 Python 语法的一个特点。 它按照它们首先出现的顺序解释位置 arguments,然后是关键字 arguments 作为下一个。 例如这段代码:

def test(a,b,c):
    print(f"Sum of all no is : {a+b+c}")
test(10,20,a=30)

返回“TypeError: test() got multiple values for argument 'a'”。 因为 Python 在位置上将 10 和 20 解释为前两个变量,所以 'a' 和 'b' 已经使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM