Syntax for one-line "nonymous" functions in "declaration style"

Antoon Pardon antoon.pardon at vub.be
Fri Mar 29 06:49:03 EDT 2019


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 

I have mixed feelings about this. I think anonymous functions only have a
place as arguments to a function. So f(x) = x * x, doesn't look like an 
anonymous function but just an other way to have a named fuction. (as
others have already pointed out)

If we really want an alternative for the lambda expresion, I would go
for the following.

f = x -> x*x (or maybe we could have the λ instead of lambda)

If we want haskell like patterns for defining functions I would go
for the following:

fac(0) -> 1
fac(n) -> n * fac(n - 1)

Which seems incompatible with the proposal for an alternative lambda. 

-- 
Antoon Pardon.




More information about the Python-list mailing list