[Python-ideas] Syntax for passing lambdas to functions

Joshua Landau joshua at landau.ws
Fri Feb 28 00:07:17 CET 2014


On 27 February 2014 18:56, Chris Angelico <rosuav at gmail.com> wrote:
> 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 [some other things]
>
> 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.

But there *is* a name. The thing is, that name might have most meaning
in context. I personally don't see why from a reader's point of view,

    callbacks["jump"]

is a worse name than

    jump_callback

IMHO, I would just force the introspection name to be the expression
in full, with the small exception of trimming *direct* dot-access;
(such that the qualified name is never worse). Therefore we could have
the names going from

    For callbacks["jump"]:
    jump_callback → callbacks["jump"]

    For myobj.myfunc (unchanged):
    myfunc → myfunc

    For myobj.callbacks[...].myfunc:
    ellipsis_myfunc_callback → callbacks[...].myfunc
    (I realise this example is contrived; it's to lower the number of examples)

and the qualified names:

    For callbacks["jump"]:
    jump_callback → callbacks["jump"]

    For myobj.myfunc:
    myfunc → myobj.myfunc

    For myobj.callbacks[...].myfunc:
    ellipsis_myfunc_callback → myobj.callbacks[...].myfunc

To me, the naming "problem" is actually a positive.


More information about the Python-ideas mailing list