[Python-ideas] Syntax for passing lambdas to functions

Chris Angelico rosuav at gmail.com
Thu Feb 27 19:56:51 CET 2014


On Fri, Feb 28, 2014 at 5:48 AM, Joshua Landau <joshua at landau.ws> wrote:
> On 27 February 2014 18:27, Antony Lee <antony.lee at berkeley.edu> wrote:
>> 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".)
>
> I would very much support this. It's actually odd that you *can't* do
> it, considering you *can* do
>
>     class A: ...
>     a = A()
>     l = [0]
>
>     for a.x in [1, 2, 3]: ...
>     for l[0] in [1, 2, 3]: ...
>
> However, there are disadvantages:
>
>     potentially ambiguous grammar
>     (very) small chance of misuse
>     could hurt introspection
>
> Further, I don't imagine I'd use it frequently. Just enough, though.

Remember, def creates a function with a name. If there's no obvious
name to attach to the function, it'd be better to either:

def functionname(...):
    ....
table[key] = functionname

or:

table[key] = lambda(...): ...

to either set a name, or not set a name, as the case may be.

ChrisA


More information about the Python-ideas mailing list