Live data from Hacker News

Stop writing lambda expressions in Python

treyhunner.com

171–180 of 202 posts

Re: Stop writing lambda expressions in Python

#171

Earlier quoted context omitted.

FWIW, in many languages, "lambda expressions" and "anonymous functions" are two similar but different things. Specifically, a lambda expression is a shorthand syntax for writing anonymous functions whose body consists of a single expression. Which is pretty much what you get in Python.

Lambdas aren't single expression functions in Lisp.

Haha, I had assumed someone would mention that.

But most Lisps are pretty rough-and-ready about these sorts of things. IIRC, Scheme requires the body of the lambda to be a single expression, doesn't it? And then you just cheat your way out of it with (begin ...).

Re: Stop writing lambda expressions in Python

#172
post #3

So in Python, imperative style is often more idiomatic than code that uses lambda expressions. While in some functional languages (Haskell) and array programming languages (J), tacit or point-free style is often more idiomatic than lambda expressions (or equivalent). There's something funny about that.

I somewhat agree but I think there are two things wrong with tacit style in Haskell: 1. It is usually taken too far (indeed I think flip is probably too far and (.).(.) is certainly too far. I’m also not a big fan of eg fmap.fmap) 2. (.) is the wrong way round. Code reads much better if it has type (a -> b) -> (b -> c) -> (a -> c) In APL or J was I don’t have enough experience to say other than that I find it difficu…

That comes down to what code is easiest to read, and that is different for different people. If playing with the APL family has taught me something, it's that what someone sees as a completely incomprehensible mess might be very readable and intuitive to another.

I personally have a hard time understanding almost any point-free Haskell code and deciphering a line of J might take me an hour. But as long as it's readable to the intended audience it's alright. Of course it isn't sometimes.

Re: Stop writing lambda expressions in Python

#173
post #83
post #41

Earlier quoted context omitted.

Guido van Rossum was explicitly against functional programming (well, in the Python language anyway). It's an imperative, object-oriented language that eventually threw functional programmers a bone with map, reduce, and filter, and that's about it.

Could you elaborate further? I wasn't there then, but what you wrote doesn't agree with my understanding. Lambda, map, reduce, and filter were part of the Python 1.0 release. The actual commit date was Tue Oct 26 17:58:25 1993 +0000: commit 12d12c5faf4d770160b7975b54e8f9b12694e012 Author: Guido van Rossum Date: Tue Oct 26 17:58:25 1993 +0000 * compile.[ch]: support for lambda() * PROTO.h, mymalloc.h: added #ifdefs fo…

Thanks, I'm probably misremembering some argument that I last heard about around 2000-2004, probably involving list comprehensions and why lambdas weren't more powerful.

Re: Stop writing lambda expressions in Python

#174

Earlier quoted context omitted.

Lambdas aren't single expression functions in Lisp.

Haha, I had assumed someone would mention that. But most Lisps are pretty rough-and-ready about these sorts of things. IIRC, Scheme requires the body of the lambda to be a single expression, doesn't it? And then you just cheat your way out of it with (begin ...).

> IIRC, Scheme requires the body of the lambda to be a single expression, doesn't it?

No, it doesn't require it.

Re: Stop writing lambda expressions in Python

#175
post #116

The crippled nature of Python's lambdas are a big weakness of Python. They sit in a no-mans land of providing half a solution but being too crippled to actually to clarity / improve programming style. I much prefer languages that go all the way and give you full integration like Ruby, ES6 and Groovy. For example, in Groovy closures just blend completely into the language: [1,2,3,4,5].grep { it > 3 }.collect { it * 5…

    [x*5 for x in [1,2,3,4,5] if x > 3]
Though yes, I agree that lambdas in python are crippled. Maybe because list comprehension exists which replaces most uses of lambda, its just that they forgot about all other uses.

Re: Stop writing lambda expressions in Python

#176

The argument used by the author for 'abs' and 'min' can also be useful for all of the functions in the operator module: https://docs.python.org/3/library/operator.html In particular 'attrgettr' and 'itemgettr' are very useful functions to map over objects and sequences.

Is attrgetter recognized by static code checkers like mypy and pycharm? One of the benefits of lambdas is that arguments autocomplete even without manual type annotations.

Re: Stop writing lambda expressions in Python

#177

Python really needs much better anonymous functions - Lambda expressions just don't cut it. To be clear, what Python does need is multiline, inline anonymous functions. In JavaScript/ES2015, the fat arrow function syntax is just remarkably powerful in its ability to simplify code. I would go so far as to say that the ES2015 fat arrow syntax completely changed the way my programs are written, increasing power and redu…

Agree. Without multi line lambdas im javascript, promises would never have been invented.

The braces also makes it easier to find the start and end of a anonymous function even if it's just a one liner, compared to pythons where its only delimited by : and ,

Re: Stop writing lambda expressions in Python

#178
You can pry them from my cold dead hands. If functions are first class objects, which they are, then they should not necessarily have a name, just like not every object created during runtime is at some point assigned to a variable.

Re: Stop writing lambda expressions in Python

#179
post #72

Personally, I use them often and disagree with a lot of this sentiment. I also disagree with a significant portion of PEP8, however. Anonymous functions are vital to any modern high level language. I think most people get hung up on the word. If the keyword was changed from “lambda” to “fun” for example I think it wouldn’t be as obtuse to more intermediate developers. To be honest I think the lambda does Python a ser…

> Anonymous functions are vital to any modern high level language. I don’t think you’re wrong, but I am also not experienced enough to know why this would be the case. What is the virtue of having an anonymous function? I get that it makes code somewhat easier to read and write if you’re only using the function once and it’s short, but that seems like an edge case. What am I missing?

The expression inside a {list,dictionary,etc} comprehension is semantically the same as an anonymous function.

Re: Stop writing lambda expressions in Python

#180

Earlier quoted context omitted.

Lambdas aren't single expression functions in Lisp.

Haha, I had assumed someone would mention that. But most Lisps are pretty rough-and-ready about these sorts of things. IIRC, Scheme requires the body of the lambda to be a single expression, doesn't it? And then you just cheat your way out of it with (begin ...).

If it did it would be annoying to type progn all the time and someone would make a macro...
Post reply on HN