[Python-3000] PEP3102 Keyword-Only Arguments
ncoghlan at gmail.com
Tue Aug 15 03:44:10 CEST 2006
Guido van Rossum wrote:
>> It would be really nice in the example above to mark ``self`` in
>> ``__call__`` as a positional only argument.
> But this is a rather unusual use case isn't it? It's due to the bound
> methods machinery. Do you have other use cases? I would assume that
> normally such wrappers take their own control arguments in the form of
> keyword-only arguments (that are unlikely to conflict with arguments
> of the wrapped method).
I'd like a syntax or convention for it so I can document the signature of
functions written in C that accept positional-only arguments using Python's
own function definition notation ;)
I'd also like to be able to use it to say "I'm not sure about this parameter
name yet, so don't rely on it staying the same!" while developing an API.
However, I'm also wondering if we need an actual syntax, or if a simple
convention would do the trick: start the names of positional-only arguments
with an underscore.
Then Steven's examples would become:
>>> class Wrapper(object):
... def __init__(self, func):
... self.func = func
... def __call__(_self, *args, **kwargs):
... print 'calling wrapped function'
... return self.func(*args, **kwargs)
def failUnlessRaises(_self, _excClass, _callableObj, *args, **kwargs):
With the 'best practice' being that any function that accepts arbitrary kwargs
should use an underscore on its named parameters.
The only way to screw the latter example up would be for a caller to do:
self.failUnlessRaises(TypeError, my_func, _callableObj=foo)
And if the 'underscore indicates positional only' convention were adopted
officially, it would be trivial for PyLint/PyChecker to flag any call that
specifies a name starting with an underscore as a keyword argument.
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-3000