[Python-3000] How to specify keyword-only arguments from C?

Guido van Rossum guido at python.org
Fri Jun 6 04:50:18 CEST 2008


On Thu, Jun 5, 2008 at 7:02 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Alexandre Vassalotti wrote:
>> Is there a way to specify keyword-only arguments using the C API. I
>> searched through the mailing list archive, read PEP 3102 and grep-ed
>> the codebase, but I found nothing looking like a C API. So I am
>> wondering, is this a Python-only feature?
>
> The C API has had this for a long time, but only in the *args, **kwds form -
> i.e., an argument is keyword only if the C code only looks for it in the
> keywords dictionary and not the args tuple.
>
> There's no special support for it in the arg parsing machinery that I know
> of, if that's the question you're asking.

I believe Alexandre is familiar with PyArg_ParseTupleAndKeywords(),
but its API doesn't let you specify keywords that have no positional
equivalent. He's after the C equivalent of

def foo(*, a, b): ...

where a and b *must* be specified as keywords by the caller. I don't
believe this is supported, due to the way
PyArg_ParseTupleAndKeywords() and friends are implemented. I don't
think it's a great loss, but if someone has the urge to implement an
API with an even longer name that supports this, I won't stop them.

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


More information about the Python-3000 mailing list