[Python-ideas] Syntax for passing lambdas to functions

Antony Lee antony.lee at berkeley.edu
Thu Feb 27 19:27:19 CET 2014


Now that someone mentioned dispatch tables, another possibility would be to
support assignment to tables and monkey-patching directly, with a syntax
like

def obj.method(*args): ... # __name__ = "method" (the attribute name)
def table[key](*args): ... # __name__ = ??? (perhaps "table[key]"?)
(and also any other "lvalue".)

Antony


2014-02-27 7:05 GMT-08:00 Ron Adam <ron3200 at gmail.com>:

>
>
> 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
>
>
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140227/1f8338bf/attachment.html>


More information about the Python-ideas mailing list