This is not an "alternative" to lambda, but rather a syntactic sugar for this particular use. There are other use cases of lambda, not expressible by this "alternative" syntax.
Further, the current lambda syntax is closer to those employed by other (purer) functional programming languages. Changing the current syntax is not a good idea.
It's proposed as "alternative syntax", which it is; but in any case, I sort of agree with you that there are probably other use cases, but I think it salient to actually provide some examples of them. The author explicitly claimed that this covers all use cases, this claim is falsifiable, the burden is on you to provide a counter-example.
This is not an "alternative" to lambda, but rather a syntactic sugar for this particular use. There are other use cases of lambda, not expressible by this "alternative" syntax.
My point is that in those other use cases are probably places you shouldn't be using lambda at all. For e.g. a bad use case for lambda might be:
avg = lambda lst: sum(lst) / len(lst)
This should be written as a def instead. But if you have good use cases of lambda, then I'd like to see them.
Further, the current lambda syntax is closer to those employed by other (purer) functional programming languages. Changing the current syntax is not a good idea.
Since Python is not a pure-functional language, why even have a lambda keyword?
Because it is very useful, and practicability beats purity in Python: both in case you presented in the article (sort's key and cmp arguments) and in other places (see my comment about csv).
The lisps, ML, Erlang etc. are far closer to lambda calculus than Python was, or will ever be. In Python, lambdas are just anonymous functions that may contain only one expression. The fact that they are called 'lambda' is an unfortunate accident.
Given Python is primarily imperative and statement oriented, I would be happier with a different way of expressing single expression functions that are occasionally useful.
I agree that Python's lambda is a bit of an eyesore, but this paragraph is complete nonsense:
The lisps, ML, Erlang etc. are far closer to lambda calculus than Python was, or will ever be. In Python, lambdas are just anonymous functions that may contain only one expression. The fact that they are called 'lambda' is an unfortunate accident.
Lambda abstractions (in lambda calculus) are exactly "just anonymous functions that may contain only one expression" and that's what they are in all functional languages.
My original comment was referring to that you use the word "pure" more loosely than what it actually means in terms of PLs.
My original comment was referring to that you use the word "pure" more loosely than what it actually means in terms of PLs.
Point taken - I shouldn't have use 'pure' but rather just claimed that Python is not philosophically a functional language.
Lambda abstractions (in lambda calculus) are exactly "just anonymous functions that may contain only one expression" and that's what they are in all functional languages.
My point however is that - in Python - lambdas are severely crippled versions of real Python functions (i.e. def). OTOH, in something like lisp, lambdas are full-power functions, except that they are anonymous.
Anyway, I'm not going to debate the syntax of other languages. Lambdas don't fit in well with the syntax or philosophy of Python. That is why Guido almost axed them from v3.0. However, given that they are occasionally handy, I am trying to find a better syntax (perhaps with limited applicability than the current lambda) which would subsume all good use cases.
27
u/vph Jul 13 '11
bad idea.
This is not an "alternative" to lambda, but rather a syntactic sugar for this particular use. There are other use cases of lambda, not expressible by this "alternative" syntax.
Further, the current lambda syntax is closer to those employed by other (purer) functional programming languages. Changing the current syntax is not a good idea.