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).
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).
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.
6
u/rcfox Sep 27 '18 edited Sep 27 '18
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.)