From: Junkman on
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? If not, would it be correct for the grammar to specify the
identifier trailing asterisk as mandatory?

Thanks for any insight.

Jay
From: Chris Rebert on
On Mon, Jul 12, 2010 at 3:32 PM, Junkman <j(a)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
From: Junkman on
A-ha! Thank you very much, Chris. Much appreciated. :-)

Jay

Chris Rebert wrote:
> On Mon, Jul 12, 2010 at 3:32 PM, Junkman <j(a)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
>