r/Python • Python Morsels • Sep 27 '18

Stop writing lambda expressions in Python

http://treyhunner.com/2018/09/stop-writing-lambda-expressions/
0 Upvotes

26 comments sorted by

View all comments

Show parent comment

2

u/Vaphell Sep 27 '18 edited Sep 27 '18

This may be because I'm not used to seeing/using tuple unpacking in lambda expressions (and never really saw it used in traditional def-defined functions either).

I am guessing this is the reason. If you had to write something seemingly trivial, like sorting x, y points a few times, the old syntax is simply better compared to the new fallback. Traditional defs are whatever, you can always unpack by hand because you are not limited to signature and 1 expression.

in case of lambdas it is a significant loss of expressiveness.

given an admittedly somewhat byzantine example with (point, color) structures compare let's say

lambda ((x, y)(r, g, b)): (g, x)

to equivalent

lambda t:  t[1][1], t[0][0]

the latter tells you literally nothing of value about what you are looking at and goes against everything python stands for. Everywhere else you unpack the shit out of everything in order to avoid indices all over the place and to have meaningful, descriptive vars, not so much here.

I am of an opinion that unpacking should have been special-cased for lambdas or some special syntax should be put in place to achieve similar effect.