Which syntax rule corresponds to the compound formula def foo (a, *, b = 10)?

The formal syntax for parameters in function definitions is as follows:

parameter_list ::=  (defparameter ",")*                                        #[1]
                    | "*" [parameter] ("," defparameter)* ["," "**" parameter] #[2]
                    | "**" parameter                                           #[3]
                    | defparameter [","] )                                     #[4]

( #[num], added by me for clarity)

Where |, according to the designation, indicates alternatives.

I don’t see how exactly it matches the function definition:

def foo(a, *, b=10): pass

An obvious rule in which it is assumed that the definition of the form def foo(a, *, b=10)will fall under #[2], which allows you to designate *for the separation of parameters only by keyword.

But the rule for foo, from what I would think, should be a combination of #[1]and #[2]:

parameter_list ::= (defparameter ",")* "*" [parameter] ("," defparameter)* ["," "**" parameter] 

#[1] #[2] , .

?

+4
2

, -, . , Python 2, :

parameter_list ::=  (defparameter ",")*
                    (  "*" identifier ["," "**" identifier]
                    | "**" identifier
                    | defparameter [","] )

, :

parameter_list ::=  (defparameter ",")*
                    ( "*" [parameter] ("," defparameter)* ["," "**" parameter]
                    | "**" parameter
                    | defparameter [","] )

-, , ( | .

+2

, , .

Python v3.5.0

funcdef: 'def' NAME parameters ['->' test] ':' suite

parameters: '(' [typedargslist] ')'

typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])*
    [',' ['*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]] | '**' tfpdef [',']]]
  | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
  | '**' tfpdef [','])

tfpdef

tfpdef: NAME [':' test]

test expression

test: or_test ['if' or_test 'else' test] | lambdef
+2

Source: https://habr.com/ru/post/1624778/


All Articles