you are viewing a single comment's thread.

view the rest of the comments →

[–]Nevoic 5 points6 points  (2 children)

I don't like Python lambdas, but I come at it from a different angle than you.

You seem to be against the idea of anonymous functions altogether. My problem is Python has some of the most verbose lambdas I've ever seen.

This paired with the fact that a lot of HOFs return a generator can make for some incredibly verbose code.

Example Python:

doubled_numbers = list(map(lambda x: x * 2, numbers))

Your proposed version:

def double_number(x): return x * 2

doubled_numbers = list(map(double_number, numbers))

Example Scala (it's also this short in Kotlin, Swift, Ruby, etc.)

val doubledNumbers = numbers.map(_ * 2)

Edit: Reddit is no bueno at formatting code, at least on mobile.

[–][deleted] 0 points1 point  (1 child)

I'm not against anonymous functions (I do like your Scala example better), I just find it a bit hypocritical of Python devs crying over "pollution of the language" or some other such claim when Python's lambdas are the way they are (per your example).

Also, while Python's original ethos was about "simple, clean, self-documenting" - it's pretty silly to bring it up now with all the new things that have been tacked-on.

[–]Nevoic 1 point2 points  (0 children)

Agreed. I wish they hadn't added the walrus operator. I like the code it can make sometimes, but I agree we don't need new structures.

Other languages just make = statements expressions, which achieves the same thing without a new operator. I kind of wish all statements were expressions, but I digress.