Python 语法错误:非默认参数跟随默认参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24719368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: non-default argument follows default argument
提问by AidanCodeX
from os import system
def a(len1,hgt=len1,til,col=0):
system('mode con cols='+len1,'lines='+hgt)
system('title',til)
system('color',col)
a(64,25,"hi","0b")
input()
When I run this, it rejects "def a(..." and highlights "(" in red. I have no clue why.
当我运行它时,它拒绝“def a(...”并以红色突出显示“(”。我不知道为什么。
采纳答案by Aaditya Ura
Let me clear two points here :
让我在这里澄清两点:
- firstly non-default argument should not follow default argument , it means you can't define (a="b",c) in function the order of defining parameter in function are :
- positional parameter or non-default parameter i.e (a,b,c)
- keyword parameter or default parameter i.e (a="b",r="j")
- keyword-only parameter i.e (*args)
- var-keyword parameter i.e (**kwargs)
- 首先非默认参数不应跟随默认参数,这意味着您不能在函数中定义 (a="b",c) 函数中定义参数的顺序是:
- 位置参数或非默认参数即 (a,b,c)
- 关键字参数或默认参数即 (a="b",r="j")
- 仅关键字参数即 (*args)
- var-keyword 参数即 (**kwargs)
def example(a, b, c=None, r="w" , d=[], *ae, **ab):
def example(a, b, c=None, r="w" , d=[], *ae, **ab):
(a,b) are positional parameter
(a,b) 是位置参数
(c=none) is optional parameter
(c=none) 是可选参数
(r="w") is keyword parameter
(r="w") 是关键字参数
(d=[]) is list parameter
(d=[]) 是列表参数
(*e) is keyword-only
(*e) 仅是关键字
(*opts) is var-keyword parameter
(*opts) 是 var 关键字参数
so first re-arrange your parameters
所以首先重新安排你的参数
- now secondary thing is you have define len1 when you are doing hgt=len1 the len1 argument is not defined when default values are saved,Python computes and saves default values when you define the function len1 is not defined, does not exist, when this happens (it exists only when the function is executed)
- 现在第二件事是你在做 hgt=len1 时定义了 len1 保存默认值时未定义 len1 参数,当你定义函数时,Python 计算并保存默认值 len1 未定义,不存在,当发生这种情况时(仅在函数执行时存在)
so second remove this "len1=hgt" its not allowed in python.
所以第二个删除这个“len1 = hgt”它在python中是不允许的。
keep in mind difference between argument and parameters.
请记住参数和参数之间的区别。
回答by Martin Konecny
You can't have a non-keyword argument after a keyword argument.
在关键字参数之后不能有非关键字参数。
Make sure you re-arrange your function arguments like so:
确保像这样重新排列函数参数:
def a(len1,til,hgt=len1,col=0):
system('mode con cols='+len1,'lines='+hgt)
system('title',til)
system('color',col)
a(64,"hi",25,"0b")
回答by falsetru
As the error message says, non-default argument til
should not follow default argument hgt
.
正如错误消息所说,非默认参数til
不应跟随默认参数hgt
。
Changing order of parameters (function call also be adjusted accordingly) or making hgt
non-default parameter will solve your problem.
更改参数顺序(函数调用也相应调整)或设置hgt
非默认参数将解决您的问题。
def a(len1, hgt=len1, til, col=0):
->
->
def a(len1, hgt, til, col=0):
UPDATE
更新
Another issue that is hidden by the SyntaxError.
SyntaxError 隐藏的另一个问题。
os.system
accepts only one string parameter.
os.system
只接受一个字符串参数。
def a(len1, hgt, til, col=0):
system('mode con cols=%s lines=%s' % (len1, hgt))
system('title %s' % til)
system('color %s' % col)