Live data from Hacker News

Stop writing lambda expressions in Python

treyhunner.com

11–20 of 202 posts

Re: Stop writing lambda expressions in Python

#11
post #4

all i could think of is "stop writing python". it seems to me that a lot of these problems are fundamental problems with python. i continue to not understand why anybody likes python the language. the fact that creating a function with lambda versus the normal way is different is bonkers. for example, in f# (and other sane languages), the following are identical: let test1 = fun x -> x * x let test2 x = x * x both re…

Nobody really likes Python the language especially much, but lots of people love Python the ecosystem, because it lets them get stuff done. I can think of a dozen languages I like better than Python, but none that's more practical in as many different contexts.

--------------------

Edit: Apparently, there are some people who do like Python the language, and some people who dislike Python the ecosystem. If you want to find a thing, just say it doesn't exist, and it will find you.

Re: Stop writing lambda expressions in Python

#12
post #4

all i could think of is "stop writing python". it seems to me that a lot of these problems are fundamental problems with python. i continue to not understand why anybody likes python the language. the fact that creating a function with lambda versus the normal way is different is bonkers. for example, in f# (and other sane languages), the following are identical: let test1 = fun x -> x * x let test2 x = x * x both re…

Nobody really likes Python the language especially much, but lots of people love Python the ecosystem, because it lets them get stuff done. I can think of a dozen languages I like better than Python, but none that's more practical in as many different contexts. -------------------- Edit: Apparently, there are some people who do like Python the language, and some people who dislike Python the ecosystem. If you want to…

I disagree... I came for the syntax and readability; I've stayed for the community.

Re: Stop writing lambda expressions in Python

#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 more often than the wacky names people often come up with. Naming things is hard!

> 3. You’re pretty sure there’s not already a function that does what you’re looking for

So the reader of the code has to know what that built-in function does. How is that much different than expecting the reader to know what a lambda is?

> 4. Everyone on your team understands lambda expressions and you’ve all agreed to use them

Or just use them and have the members of your team better themselves by learning a useful language feature.

---

In my experience, names lie--functions don't. If a developer isn't going to spend the time to write a nice comment, they aren't going to use a comprehensible naming convention. In trivial cases where naming is easy, the lambda is also usually very easy to parse.

Re: Stop writing lambda expressions in Python

#14
post #4

all i could think of is "stop writing python". it seems to me that a lot of these problems are fundamental problems with python. i continue to not understand why anybody likes python the language. the fact that creating a function with lambda versus the normal way is different is bonkers. for example, in f# (and other sane languages), the following are identical: let test1 = fun x -> x * x let test2 x = x * x both re…

One of the python motto's is "explicit is better than implicit". Not having to pattern-parse the structures makes programs much easier to read for some people (lke me).

But the point is, even with f# syntax, most of the blog post would still apply (needless function calls, non-trivial functions, multiline-unpacking and so on)

I do not agree with some of the premises of this post, but saying that changing lambda syntax will somehow help is just not true.

Re: Stop writing lambda expressions in Python

#15

Earlier quoted context omitted.

Nobody really likes Python the language especially much, but lots of people love Python the ecosystem, because it lets them get stuff done. I can think of a dozen languages I like better than Python, but none that's more practical in as many different contexts. -------------------- Edit: Apparently, there are some people who do like Python the language, and some people who dislike Python the ecosystem. If you want to…

I disagree... I came for the syntax and readability; I've stayed for the community.

I suppose I like the readability too, but there are a number of things about the language I would rather had been done differently; I still insist that its approach to scope is worse than any choice except making everything global.

Re: Stop writing lambda expressions in Python

#16
post #4

all i could think of is "stop writing python". it seems to me that a lot of these problems are fundamental problems with python. i continue to not understand why anybody likes python the language. the fact that creating a function with lambda versus the normal way is different is bonkers. for example, in f# (and other sane languages), the following are identical: let test1 = fun x -> x * x let test2 x = x * x both re…

Nobody really likes Python the language especially much, but lots of people love Python the ecosystem, because it lets them get stuff done. I can think of a dozen languages I like better than Python, but none that's more practical in as many different contexts. -------------------- Edit: Apparently, there are some people who do like Python the language, and some people who dislike Python the ecosystem. If you want to…

in my experience, the python ecosystem is a mess. yes, there are lots of libraries, but that is just one part of the ecosystem. for one, everyone uses a different distribution, whether it's anaconda, python(x,y), or just the default python install. this creates a lot of fragmentation, and it's hard to come into a project and fix the mess that's been created. then there's the 2.x vs 3.x issue that is still a problem. i have been introduced to three python projects at various times in my work, and all still used python 2.7 and even python 2.6. (these were projects that i helped out on and didn't create or own.) in each case, python made it quite difficult to even understand if you had setup the environment correctly.

but yes, there are lots of libraries. i think people need to take a stance and simply pull those libraries into other languages (which many are already) or simply realize that most are just wrappers around some other language library, which can be easily done in other languages as well.

Re: Stop writing lambda expressions in Python

#17
The benefit of purely functional programming is that side effects can be managed explicitly and checked by the compiler, speeding up code refactors and making them safer. Using high order functions to save a few lines of code is not the big benefit of functional programming and is more a superficial feature more akin to syntax sugar. Same with list comprehensions. This seems to be the case in many hybrid languages that borrow superficial purely functional language features.

Re: Stop writing lambda expressions in Python

#18
post #4

all i could think of is "stop writing python". it seems to me that a lot of these problems are fundamental problems with python. i continue to not understand why anybody likes python the language. the fact that creating a function with lambda versus the normal way is different is bonkers. for example, in f# (and other sane languages), the following are identical: let test1 = fun x -> x * x let test2 x = x * x both re…

Nobody really likes Python the language especially much, but lots of people love Python the ecosystem, because it lets them get stuff done. I can think of a dozen languages I like better than Python, but none that's more practical in as many different contexts. -------------------- Edit: Apparently, there are some people who do like Python the language, and some people who dislike Python the ecosystem. If you want to…

> Nobody really likes Python the language especially much

I beg to differ. I like Python the language. I also like Python the ecosystem.

Re: Stop writing lambda expressions in Python

#19

> When I encounter a lambda expression in the wild, I often find that removing it improves code readability. I have never seen a code that wasn't made clearer when moving from a three-liner non-lambda somewhere upper in the code, to a one-liner lambda used at the right place

I wouldn't say never, but I'd say the vast majority of the time this has been the case for me as well.

Re: Stop writing lambda expressions in Python

#20
post #8

The first two situations where he recommends avoiding lambda I would agree with. But when he assigns `colors_by_length`, that's a perfectly valid usage of lambda. It's actually an ideal example of why it exists. His `length_and_alphabetical` is not an improvement. The tuple unpacking is something that destructuring in lambda syntax would improve, as it did with JavaScript. In ES6 it would be: const points = [[[1, 2],…

Python 2.7 had tuple parameter unpacking, but it was removed in Python 3.

https://www.python.org/dev/peps/pep-3113/

Post reply on HN