Live data from Hacker News

Stop writing lambda expressions in Python

treyhunner.com

151–160 of 202 posts

Re: Stop writing lambda expressions in Python

#151
Great post, love all the examples categorizing types of misuse (at least what he deems as misuse, and I won't argue because I have a lot to learn by reading through this).

Reminds me of a story.

A few years ago Peter Norvig (Google) posted a "Show HN" posts with one of his amazing notebook excursions where he went through nice little problem, building up a library of functions to make a solution. I think this has happened multiple times.

One such time, one bit of code used a lambda expression, and I was new to Python, and wondered in a possibly-slightly-whiney comment (as I recall) whether it was the most readable way to do things.

Within an hour or so, his post was silently updated and the lambda keyword was gone. I don't remember the nature of the rest of the change, whether it was just the one block of code or more, but the code was more readable to me, at least as a newbie, at the time. Not sure my comment had anything to do with it, but I was impressed that he did make the change in any case. I have never worked with him, but my respect for him grew with that tiny little incident.

BTW in case anyone feels the urge to explain lambda expressions to me now, that's OK, no need, but thanks anyway.

Re: Stop writing lambda expressions in Python

#152
post #93

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.

Which languages?

Always heard them called lambda expressions in c++.

Re: Stop writing lambda expressions in Python

#153
post #128

Earlier quoted context omitted.

It's not so much naming the function, but that doing so moves it outside the flow of the logic it is participating in. Especially when it's going to reference local context that is particularly confusing. There are definitely times when extracting out a function and naming it is the right thing to do. But there are an equal number of times when keeping all the local context together is better. It's probably hard to a…

I find it tremendously helpful to move the function out of the flow of logic so it is modularized. When I read scala or haskell code where as soon as you see map or filter or something you know you’re gonna get some crazy anonymous function to follow, I get sad because it’s a miserable and confusing way to write code instead of pulling the function out into a separate definition with documentation, and then having th…

> I get sad because it’s a miserable and confusing way to write code

you just have to visualize it as a tree

Re: Stop writing lambda expressions in Python

#154

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…

Yes and the fact that they aren’t full closures is a travesty - I can’t tell you how many times a nice dependency injection has been ruined by having to add a bunch of def’s. It bloats code and reduces expressiveness.

Re: Stop writing lambda expressions in Python

#155

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…

Python is fundamentaly flawed, since it's based on lines (and significant indentation), and foremost, since it's based on statements, and not only on expressions.

Lambda expressions look in python like a sore spot, because Python stresses so much statements, and procedural programming.

IIRC, recently we've seen passing a paper in hacker news, that let beotians write programs, and only ~10% IIRC (or less) of their uterances were procedural statements!

Re: Stop writing lambda expressions in Python

#156
post #154

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…

Yes and the fact that they aren’t full closures is a travesty - I can’t tell you how many times a nice dependency injection has been ruined by having to add a bunch of def’s. It bloats code and reduces expressiveness.

Nested functions in Python are "full closures" in every sense I understand it. Is there something in missing?

They can read the value of local variables (including arguments) in the parent function, and write to them (using the "nonlocal" statement). If one nested function assigns a new value to a captured variable then, of course, this change is seen by all other nested functions and the enclosing function. Most importantly, if the lifetime of the nested function object is longer than the runtime of the enclosing function (most commonly because you return the nested function object from the enclosing function) then the lifetime of the enclosing function's local variables is extended appropriately.

This applies to both nested functions defined using a "def" statement and nested functions defined using a "lambda" expression (except you can't assign to variables in a lambda expression). I say this because you don't say what you mean by "they", and it's a common misconception that only lambda expressions capture enclosing variables.

The only disadvantage of this is that, because name lookup is done at execution time rather than parsing time, all the enclosing variables must be captured and their lifetimes extended even if the nested function doesn't use any of them. In this sense, nested functions are too closurey in Python! This is not really so bad because if you don't need variable capture then you should simply not be using a nested function in the first place; this is best anyway because it means you are sharing one function object between different uses.

Re: Stop writing lambda expressions in Python

#157

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…

This is the number one thing that makes it difficult for me to want to switch from JavaScript to Python despite Python otherwise lining up almost perfectly with my programming/syntax preferences.

JavaScript has things like TypeScript (and Babel in general) that add features that are "missing" from the language. Is there anything similar for Python and anonymous functions?

Re: Stop writing lambda expressions in Python

#158

Earlier quoted context omitted.

I find it tremendously helpful to move the function out of the flow of logic so it is modularized. When I read scala or haskell code where as soon as you see map or filter or something you know you’re gonna get some crazy anonymous function to follow, I get sad because it’s a miserable and confusing way to write code instead of pulling the function out into a separate definition with documentation, and then having th…

> I get sad because it’s a miserable and confusing way to write code you just have to visualize it as a tree

Yes, that is a confusing way to write and read code, rather than a linear flow, like flattening the tree by extracting functions into separate definitions.

Re: Stop writing lambda expressions in Python

#159

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…

Python is fundamentaly flawed, since it's based on lines (and significant indentation), and foremost, since it's based on statements, and not only on expressions. Lambda expressions look in python like a sore spot, because Python stresses so much statements, and procedural programming. IIRC, recently we've seen passing a paper in hacker news, that let beotians write programs, and only ~10% IIRC (or less) of their ute…

I've seen people make the distinction between statements and expressions in the past, but I don't get the significance. I mean I understand what they are but I don't understand why people view the concept as such a big deal.

Are there any resources explaining this which you would recommend?

Re: Stop writing lambda expressions in Python

#160
post #13

> I’d say that using lambda expressions is acceptable only if your situation meets all four of these criteria: > 1. The operation you’re doing is trivial: the function doesn’t deserve a name Sure. > 2. Having a lambda expression makes your code more understandable than the function names you can think of This is just vague. Locally defining a function right where you need it makes code much more understandable to me…

I've noticed a pattern in among python developers where the faux pas of assigning a lambda is used to signify "I'm going to use this function in a vaguely functional programming manner". For example, by passing it into map, or filter, or sorted. For this reason, I don't really mind it. It's a sort of cue to the reader about the style of programming that will follow.

I many of those cases people use lambda when a partial would be a better fit.
Post reply on HN