Live data from Hacker News

Stop writing lambda expressions in Python

treyhunner.com

111–120 of 202 posts

Re: Stop writing lambda expressions in Python

#111
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.

Re: Stop writing lambda expressions in Python

#112
post #86

Earlier quoted context omitted.

Also, wouldn't the existence of a library function (#3) imply that what you're doing is either non-trivial or common enough that someone has already given the function a name? Seems like #1 is sufficient.

To give an example based on the article, I find it cumbersome to use itemgetter. I could use the library function: import operator sort(values, key=operator.itemgetter(2)) but it seems decidedly trivial and common to use a lambda: sort(values, key=lambda x: x[2])

The first returns a callable, the second a value.

Re: Stop writing lambda expressions in Python

#113
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…

"i continue to not understand why anybody likes python the language." As someone who has had a lifetime of on-off relationships with attempting to become a competent programmer, (BASIC, assembler, C, Pascal, Visual Basic, C++ and others that I can no longer remember), Python is the only language that has helped me become anywhere near useful or proficient as a programmer. I've managed to build a couple of -genuinely…

> I'm not going to be writing 3D games or real-time audio apps in it, but for what I've been trying to do (solve real-world problems)

TIL 3D games and real-time audio apps are not "real-world problems".

Re: Stop writing lambda expressions in Python

#114
post #102

Earlier quoted context omitted.

i guess that is true in python3 but it is not in python 2.7.

False. You're just not familiar with how the REPL works. >>> None >>> print(None) None In the future, you might want to refrain from strong criticism of a thing you're not familiar with. That habit leads to all sorts of -isms.

i do know how the repl works. i explicitly tried it out. i defined the function just as i said and ran

  test(2) is None
and got false. i just now went to try it out again at home and got true. i don't know what to tell you other than something must have got shadowed somewhere.

Re: Stop writing lambda expressions in Python

#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 }
Compare to python is almost incomprehensible:

    map(lambda y: y * 5, filter(lambda x: x > 3, [1,2,3,4,5]))
But breaking this into functions reduces to an almost silly level of verbosity. Of course, idiomatic python would write this as a list comprehension, but that is just proof that the lambda construct is broken, and the list comprehension is a band-aid and doesn't scale up to more complex scenarios.

Re: Stop writing lambda expressions in Python

#117

Earlier quoted context omitted.

Amen, brother. I'm not proficient in Python, but from my (admittedly, limited) experience with it, it feels like programming with a straightjacket on.

It is, and that's the point. The Python community coined TOOWTDI (there's only one way to do it) in response to Perl's "There's more than one way to do it". What that way is seems to be "what even the most clueless programmer could reasonably understand". This to the point where even the adoption of a concept as common as assignment expressions was met with a community shitstorm. It seems like a language designed for…

> It seems like a language designed for people who really have no interest in craft of programming, and just use it as a means to an end

It's kind of funny how much that echos the past criticisms of Java - a language lacking in all kinds of features that frustrate advanced programmers. Yet Python's popularity was in many ways a reaction against Java - perhaps for other reasons.

Re: Stop writing lambda expressions in Python

#118
post #68

Earlier quoted context omitted.

Thanks for the link. The paragraph you quote makes me think he is comparing Python's lambda to Scheme's equivalent, which of course will make Python's lambda seem broken. IIRC the course was taught in Scheme at one time, but it switched to Python because it seemed easier for students to grasp the basics that way.

It's not just Scheme; in Common Lisp and Haskell and every other language that supports lambda, lambda is a first-class construct that is used all the time. Python's lambda feels like a war veteran that had its legs blown off. Best policy is probably just to pretend it's not in the language.

> It's not just Scheme

Agreed. I only mentioned Scheme specifically because Abelson did in what was quoted.

Re: Stop writing lambda expressions in Python

#119
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…

Guido seemingly has a strong distaste for functional ideas, so anything that comes from that paradigm ends up getting an almost half-assed implementation.

See also: map and filter being the only 2 other functions implemented and both of them being less performant than their imperative equivalents; outright denial of any attempt at TCO because "Guido doesn't like recursion".

Re: Stop writing lambda expressions in Python

#120
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…

man... i totally agree these things can be abused. co-worker was rewriting a project and went a little bit wild but i found the code pretty readable (for an experienced coder). You can do some pretty sweet stuff using built in python methods. We had a combination of dict/zip/map/lambda that run a ton of processing code and built a pretty complicated JSON-like structure in 4 lines. I personally liked it, you can tell some thought was put into the code rather then blasting out first solution and using that in production
Post reply on HN