SyntaxError:positional argument follows keyword argument
目录
SyntaxError:positional argument follows keyword argument
欢迎来到英杰社区https://bbs.csdn.net/topics/617804998
【常见模块错误】
如果出现模块错误
进入控制台输入:建议使用国内镜像源
pip install 模块名称 -i https://mirrors.aliyun.com/pypi/simple
我大致罗列了以下几种国内镜像源:
清华大学
https://pypi.tuna.tsinghua.edu.cn/simple
阿里云
https://mirrors.aliyun.com/pypi/simple/
豆瓣
https://pypi.douban.com/simple/
百度云
https://mirror.baidu.com/pypi/simple/
中科大
https://pypi.mirrors.ustc.edu.cn/simple/
华为云
https://mirrors.huaweicloud.com/repository/pypi/simple/
腾讯云
https://mirrors.cloud.tencent.com/pypi/simple/
【解决方案】
在Python中,函数调用时参数的顺序和类型有严格的规则。根据搜索结果中的,我们可以明确地了解到这些规则:
- 位置参数(Positional arguments):这些参数是按照它们在函数定义中的位置来匹配的,不需要指定参数名。
- 关键字参数(Keyword arguments):这些参数需要通过参数名来指定其值,可以出现在位置参数之后。
然而,存在一个重要的规则:位置参数必须位于关键字参数之前。如果违反这一规则,将引发SyntaxError
错误,即“位置参数跟随关键字参数”。
例如,在中提到的代码片段:
def fun(x,y):
print(x,y)
fun3(y=4,3)
这里,fun3(y=4,3)
中的3
是一个位置参数,而y=4
是一个关键字参数。按照规则,位置参数应该在关键字参数之前,因此这段代码会引发SyntaxError
。
同样,在中也提到了这一点:
因此,当遇到SyntaxError: positional argument follows keyword argument
错误时,应检查函数调用中的参数顺序,确保所有位置参数都位于关键字参数之前。