Syntax for one-line "nonymous" functions in "declaration style"
Alexey Muranov
alexey.muranov at gmail.com
Wed Mar 27 15:33:58 EDT 2019
On mer., Mar 27, 2019 at 5:00 PM, python-list-request at python.org wrote:
> On 27/03/19 09:21, Alexey Muranov wrote:
>> Whey you need a simple function in Python, there is a choice
>> between a
>> normal function declaration and an assignment of a anonymous
>> function
>> (defined by a lambda-expression) to a variable:
>>
>> def f(x): return x*x
>>
>> or
>>
>> f = lambda x: x*x
>>
>> It would be however more convenient to be able to write instead just
>>
>> f(x) = x*x
>>
>> (like in Haskell and such).
>>
>> Have this idea been discussed before?
>>
>> I do not see any conflicts with the existing syntax. The following
>> would also work:
>
> I don't know. Something like the following is already legal:
>
> f(x)[n] = x * n
>
> And it does something completly different.
>
Thanks for pointing out this example, but so far i do not see any issue
with this.
Of course assignment (to an identifier) is a completely different type
of operation than in-place mutation (of an object) with __setitem__,
etc.
In
<...> [<...>] = <...>
the part to the left of "[<...>]=" is an expression that is to be
evaluated, and only its value matters. Here "[]=" can be viewed as a
method call, which is distinguished by the context from "[]" method
call (__getitem__).
In
<identifier> = <...>
the <identifier> is not evaluated.
I still think that
<identifier>(<identifiers>)...(<identifiers>) = <...>
is unambiguous. The following seems possible too:
a[m][n](x)(y) = m*x + n*y
It would be the same as
a[m][n] = lambda x: lambda y: m*x + n*y
Here a[m] is evaluated, and on the result the method "[]="
(__setitem__) is called.
Basically, "()...()=" seems to technically fit all contexts where "="
fits...
Alexey.
More information about the Python-list
mailing list