Functions with positional only arguments

This came from a discussion on the python-ideas mailing list. The file posonly.py(download here) defines a function posonly that can be used to decorate functions so that their arguments become positional only (i.e. they can't be given as keywords). Below are a few examples of how this can be used.

Here are some examples of use:

>>> @posonly
... def f(x, y=1): return x, y
... 
>>> f(1)
(1, 1)
>>> f(1, y=2)
Traceback (most recent call last): File "", line 1, in ? TypeError: f() got an unexpected keyword argument 'y'
>>> f(x=1)
Traceback (most recent call last): File "", line 1, in ? TypeError: f() got an unexpected keyword argument 'x'
>>> @posonly ... def update(self, container=None, **kwargs): ... "Pretend function" ... return self, container, kwargs ... >>> # self and container can be used as keyword argument names ... update('self', 'container', self=1, container=2)
('self', 'container', {'self': 1, 'container': 2})
>>> # There is still a way to access posonly args by name... ... f(**{'x@':'spam'})
('spam', 1)
>>> # @posonly preserves __doc__ and __name__ ... # Positional only arguments are followed by a '@' ... help(update)
Help on function update in module __main__: update(self@, container@, **kwargs) Pretend function
>>>
Last updated on Tue Oct 28 16:16:12 2008
arno AT marooned.org.uk