Python 3 grammar, function parameters

Junkman j at junkwallah.org
Tue Jul 13 13:48:50 EDT 2010


A-ha!  Thank you very much, Chris.  Much appreciated.  :-)

Jay

Chris Rebert wrote:
> On Mon, Jul 12, 2010 at 3:32 PM, Junkman <j at junkwallah.org> wrote:
>> Greetings to Python users,
>>
>> I'm trying to parse Python code using the grammar supplied with the
>> documentation set, and have a question on the grammar for function
>> parameters:
>>
>> funcdef: 'def' NAME parameters ['->' test] ':' suite
>> parameters: '(' [typedargslist] ')'
>> typedargslist: ((tfpdef ['=' test] ',')*
>>                ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef]
>> | '**' tfpdef)
>>                | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
>> tfpdef: NAME [':' test]
>>
>> >From what I understand, a naked asterisk - i.e. it is not a prefix to an
>> identifier - is not a valid parameter, but the grammar  explicitly
>> allows it by making the  identifier that immediately follows the
>> asterisk optional.
>>
>> Are there cases where naked asterisk is allowed as a function
>> parameter?
> 
> Yes, for keyword-only arguments, a new feature in Python 3.x. See PEP
> 3102 (http://www.python.org/dev/peps/pep-3102/ ).
> A lone asterisk signals that the function does not take extra
> positional arguments. All keyword-only arguments must be declared
> after a lone or normal *-argument.
> 
> Example:
> def compare(a, b, *, key=None):
> 
> compare() does not accept extra positional arguments and has 1
> keyword-only argument (namely, `key`).
> 
> Cheers,
> Chris
> --
> http://blog.rebertia.com
> 




More information about the Python-list mailing list