Alternative to multi-line lambdas: Assign-anywhere def statements

Ian Kelly ian.g.kelly at gmail.com
Thu Feb 12 11:17:23 EST 2015


On Wed, Feb 11, 2015 at 8:56 PM, Chris Angelico <rosuav at gmail.com> wrote:
> On Thu, Feb 12, 2015 at 1:57 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> The reason I don't like this replacing def isn't because the name is
>> necessarily lost. It's because the lack of the well-defined def
>> statement encourages more complex usages like
>>
>>     functions['f'] = x -> x**2
>>
>> where such implicit transformations won't work.
>
> That's actually where this started. I mean, if a function can be
> called "<lambda>", why can't it be called "functions['f']"?

If the name isn't an identifier then it will make it harder to look up
the function from the name via introspection. Another thing that might
cause problems is decoration:

    say_hello = classmethod(cls -> print("Hello from %s" % cls.__name__))

How would the name say_hello make it all the way onto the function
object here? I suppose there could be a syntax like:

    @classmethod
    say_hello = cls -> print("Hello from %s" % cls.__name__)

But now the assignment statement has to be treated grammatically as a
special form of assignment, just like a def statement.



More information about the Python-list mailing list