Securing a future for anonymous functions in Python

Steven Bethard steven.bethard at gmail.com
Thu Dec 30 23:14:31 EST 2004


Jeff Shannon wrote:
> My thesis here is that one of the most common (legitimate) uses of 
> lambda is as an adapter, to create an intermediary that allows a 
> callable with a given signature to be used in places where a different 
> signature is expected -- that is, altering the number or order of 
> arguments passed to a given callable (and possibly also capturing the 
> current value of some other variable in the process).  I feel that it's 
> more fruitful to focus on this "adapter" quality rather than focusing on 
> the "anonymous function" quality.

Maybe the 'functional' module proposed in PEP 309[1] could provide such 
functions?

py> def ignoreargs(func, nargs, *kwd_names):
...     def _(*args, **kwds):
...         args = args[nargs:]
...         kwds = dict((k, kwds[k])
...                     for k in kwds if k not in kwd_names)
...         return func(*args, **kwds)
...     return _
...
py> def f(x, y):
... 	print x, y
...
py> ignoreargs(f, 2)(1, 2, 3, 4)
3 4
py> ignoreargs(f, 2, 'a', 'b')(1, 2, 3, 4, a=35, b=64)
3 4

Steve

[1] http://python.fyxm.net/peps/pep-0309.html



More information about the Python-list mailing list