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

6

u/rcfox Sep 27 '18 edited Sep 27 '18

Tuple unpacking can improve readability over using hard-coded index lookups. Using lambda expressions often means sacrificing some Python language features, specifically those that require multiple lines of code (like an extra assignment statement).

Rebuttal: points_by_color = sorted(points, key=lambda ((x, y), color): color)

Overall agree though.

Edit: Looks like tuple parameter unpacking was removed in Python 3. https://www.python.org/dev/peps/pep-3113/ (I'm still using 2.7 for work, so that's my default.)

3

u/Vaphell Sep 27 '18

Looks like tuple parameter unpacking was removed in Python 3

I am still a bit salty about this in the context of lambdas. indexing into tuples as opposed to unpacking is bullshit and the supposedly more elegant itemgetter whatever is bullshit too.

1

u/Sporke Sep 27 '18

I was hoping the author would mention operator.itemgetter(1) in that case

1

u/__deerlord__ Sep 27 '18

Can you explain whats going on with this lamda? Same as

def f(t, color):

where t is the 2-tuple (x,y)?

1

u/rcfox Sep 27 '18

No, it would be the same as:

def f(point):
    (x, y), color = point
    return color

points is a list of tuples, and the key function only takes a single argument. Python 2.7 allows you to unpack an iterable in the place of a parameter.

2

u/__deerlord__ Sep 27 '18

Is it a list of 2-tuples? Can you provide an example of what point is, and then what it unpacks to for (x,y), color?

Because the following works on py3

c = [(1,2), (3,4)]
(x,y), color = c
x
>>> 1
y
>>> 2
color
>>> (3,4)

1

u/rcfox Sep 27 '18

From the article: points = [((1, 2), 'red'), ((3, 4), 'green')]

The thing that doesn't work in Python 3 is unpacking in the parameter list. ie:

def foo(((x, y), color)): 
   return color

colors = map(foo, points)

The above does work in Python 2.7 but not Python 3.

1

u/__deerlord__ Sep 28 '18

It sounds like this implicitly uses *args, but that in python3 you can use itertools.starmap for this. Explicit > implicit.

Note: I havent played with this, though, too late here.

1

u/treyhunner Python Morsels Sep 27 '18

Yes that was removed in Python 3. I feel like that syntax was actually particularly confusing with lambda expressions though.

When I first glanced at that code I assumed a dictionary was involved somehow or maybe incorrect syntax because my brain didn't parse the tuples before the `:` as arguments.

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).

3

u/rcfox Sep 27 '18

I think the parameter unpacking is quite nice if you think of it as a limited form of pattern matching. I can definitely understand why they removed it from the language though.

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.