[英]SyntaxError: positional argument follows keyword argument
I am wondering if the below statement is correct.我想知道下面的说法是否正确。
SyntaxError: positional argument follows keyword argument SyntaxError:位置参数跟随关键字参数
I know what is positional argument and keyword argument but getting confused with the above statement.我知道什么是位置参数和关键字参数,但对上述语句感到困惑。 for example when we say
例如当我们说
A follows B It means A 跟随 B 这意味着
//It means that “A” comes after “B” //这意味着“A”在“B”之后
So in the same way when we are calling any function then we should pass the positional argument first and then the keyword argument.因此,以同样的方式,当我们调用任何 function 时,我们应该先传递位置参数,然后传递关键字参数。 And in that case, correct statement should be
在这种情况下,正确的陈述应该是
***SyntaxError: keyword argument follows positional argument***
example:例子:
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
example2:示例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
That's a feature of Python syntax.这是 Python 语法的一个特点。 It interprets positional arguments in the order in which they appear first and then followed by the keyword arguments as next.
它按照它们首先出现的顺序解释位置 arguments,然后是关键字 arguments 作为下一个。 For example this code:
例如这段代码:
def test(a,b,c):
print(f"Sum of all no is : {a+b+c}")
test(10,20,a=30)
Returns "TypeError: test() got multiple values for argument 'a' ".返回“TypeError: test() got multiple values for argument 'a'”。 Because Python positionally interprets 10 and 20 as the first two variables, so 'a' and 'b' are already used
因为 Python 在位置上将 10 和 20 解释为前两个变量,所以 'a' 和 'b' 已经使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.