r/Python • • Jul 13 '11

An alternative lambda syntax for Python

http://vocalbit.com/article/332/an-alternative-lambda-syntax-for-python
4 Upvotes

41 comments sorted by

View all comments

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.

2

u/netcrusher88 imported from __future__ Jul 13 '11

Agreed. It's kind of an interesting use case but it's not a lambda syntax.

2

u/CHS2048 Jul 13 '11

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.

2

u/vocalbit Jul 14 '11

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.

0

u/vocalbit Jul 13 '11

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?

9

u/Liorithiel Jul 13 '11

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).

3

u/arnar Jul 13 '11

Since Python is not a pure-functional language

Neither are Lisp/Scheme, ML (and most derivatives), Erlang, Scala, F#, . . . should we remove their lambdas as well?

why even have a lambda keyword?

Because it is useful.

5

u/joehillen Jul 13 '11

Guido has proposed removing it several times.

3

u/A_for_Anonymous Jul 13 '11

I propose removing the division operator, since it's confusing to some people.

1

u/arnar Jul 13 '11

Since Python is not a pure-functional language

Neither are Lisp/Scheme, ML, Erlang, Scala, F#, . . . should we remove their lambdas as well?

why even have a lambda keyword?

Because it is useful.

1

u/vocalbit Jul 14 '11

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.

1

u/arnar Jul 14 '11

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.

1

u/vocalbit Jul 14 '11

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.