The formal syntax用于函数定义中的参数如下:
parameter_list ::= (defparameter ",")* #[1]
| "*" [parameter] ("," defparameter)* ["," "**" parameter] #[2]
| "**" parameter #[3]
| defparameter [","] ) #[4]
(为了清楚起见,我添加了#[num]
)
Where |
, according to the notation, indicates alternatives.
我无法确定它与以下函数定义的匹配程度如何:
def foo(a, *, b=10): pass
显而易见的规则是假设def foo(a, *, b=10)
形式的定义属于#[2]
,这允许*
符号分隔仅关键字参数。
但根据我的想法,foo
的规则必须是#[1]
和#[2]
的组合:
parameter_list ::= (defparameter ",")* "*" [parameter] ("," defparameter)* ["," "**" parameter]
由于规则#[1]
和#[2]
似乎完全不符合本案。
我在这里缺少什么?
答案 0 :(得分:2)
这是为Python v3.5.0中的function declaration syntax定义实际语言语法的方式
funcdef: 'def' NAME parameters ['->' test] ':' suite
parameters: '(' [typedargslist] ')'
typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])*
[',' ['*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | '**' tfpdef [',']]]
| '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
| '**' tfpdef [','])
tfpdef: NAME [':' test]
和test
与expression
test: or_test ['if' or_test 'else' test] | lambdef
答案 1 :(得分:2)
形式语法似乎缺少左括号。最后一个关闭的东西与任何东西都不匹配,如果你看the Python 2 version,那就完全合情合理了:
parameter_list ::= (defparameter ",")*
( "*" identifier ["," "**" identifier]
| "**" identifier
| defparameter [","] )
所以请把它读作:
parameter_list ::= (defparameter ",")*
( "*" [parameter] ("," defparameter)* ["," "**" parameter]
| "**" parameter
| defparameter [","] )
有人可能会在调整事物时将(
替换为|
。