[Python-Dev] funcdef grammar production

Guido van Rossum guido at python.org
Mon Mar 15 15:10:32 EST 2004


> PEP 318 is likely to change the BNF productions for funcdef,
> so I was taking a look at what is there right now.
> 
>   funcdef ::= "def" funcname "(" [parameter_list] ")" ":" suite
>   parameter_list ::= (defparameter ",")*
>                         ("*" identifier [, "**" identifier] |
> 				 "**" identifier | defparameter [","])
>   defparameter ::= parameter ["=" expression]
>   sublist ::= parameter ("," parameter)* [","]
>   parameter ::= identifier | "(" sublist ")"
>   funcname ::= identifier
> 
> But the text points out that no purely positional (without a 
> default value) arguments can occur after any keyword arguments.
> Is there a reason not to include this in the BNF?
> 
> (Assuming the other productions are unchanged,) this seems to work:
> 
>   parameter_list ::=  starparameter  |
>      defparameter ("," defparameter)* ["," [starparameter]] |
>      parameter ("," parameter)* ("," defparameter)* ["," [starparameter]]
> 
>   starparameter ::= "*" identifier [, "**" identifier] | 
>                     "**" identifier
> 
>   defparameter ::= parameter "=" expression

This can't work (at least not in the parser -- I don't care what's put
in the docs) because parameter and defparameter have the same set of
initial tokens.

> starparameter was separated out because a trailing comma is permitted
> after a positional or default parameter, but not after *args or **kwargs.
> 
> Is there some technical reason not to do this?

Not to do what?  Allow a comma after *args?

> For instance, is the doco autogenerated from the actual code?

You should know by now that there are various tools for that, although
again maybe you're asking the question in a different context.

> Is it somehow cheaper to check for an optional first parameter than
> for three possibilities?

Who cares?

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list