[Python-ideas] Syntax for passing lambdas to functions
Ron Adam
ron3200 at gmail.com
Thu Feb 27 16:05:02 CET 2014
On 02/27/2014 04:17 AM, Nick Coghlan wrote:
> > For what it's worth, the following *could* be made to work:
> >
> > spam() = fire_the_ducks(42)
> > f(onclick = spam)
>
> Let's talk about that for a moment. It would be a matter of making this:
>
> NAME(ARGSPEC) = EXPR
>
> syntactic sugar for this:
>
> def NAME(ARGSPEC): return EXPR
>
> Not what you would call a big win.
A good potential use of lambda is in constructing a dispatch table from a
dictionary litteral or constructor.
select = {key1:callable, key2:callable, ...}
result = select[k]()
select = dict(key1=callable, key2=callable, ...)
result = select[k]()
And there is also the case of it working in a ternary if...
result = (callable if k else callable)() #k is True or False
I think these are good test cases for evaluating usability and readability.
It would be nice if it worked well for each of these.
Cheers,
Ron
More information about the Python-ideas
mailing list