疑问:SyntaxError提示‘positional argument follows keyword argument’是否准确
关于Python语法错误提示‘SyntaxError: positional argument follows keyword argument’的困惑
我清楚位置参数(positional argument)和关键字参数(keyword argument)的概念,也知道函数调用时应该先传递位置参数,再传递关键字参数。但我对报错提示‘SyntaxError: positional argument follows keyword argument’感到困惑——按照“A follows B”表示“A在B之后”的逻辑,正确的提示似乎应该是‘SyntaxError: keyword argument follows positional argument’才对?
示例1:错误调用触发报错
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
这个示例中,因为先传递了关键字参数a=10,再传递位置参数20,触发了上述语法错误提示。
示例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
先传递位置参数20,再传递关键字参数,函数就能正常执行并输出结果。
内容的提问来源于stack exchange,提问作者Shailesh Yadav
相关产品推荐
相关产品推荐

