Asked By – balki
Consider the following python2 code
In : points = [ (1,2), (2,3)] In : min(points, key=lambda (x, y): (x*x + y*y)) Out: (1, 2)
This is not supported in python3 and I have to do the following:
>>> min(points, key=lambda p: p*p + p*p) (1, 2)
This is very ugly. If the lambda was a function, I could do
def some_name_to_think_of(p): x, y = p return x*x + y*y
Removing this feature in python3 forces the code to either do the ugly way(with magic indexes) or create unnecessary functions(The most bothering part is to think of good names for these unnecessary functions)
I think the feature should be added back at least to lambdas alone. Is there a good alternative?
Update: I am using the following helper extending the idea in the answer
def star(f): return lambda args: f(*args) min(points, key=star(lambda x,y: (x*x + y*y))
Update2: A cleaner version for
import functools def star(f): @functools.wraps(f) def f_inner(args): return f(*args) return f_inner
Now we will see solution for issue: What is the good python3 equivalent for auto tuple unpacking in lambda?
No, there is no other way. You covered it all. The way to go would be to raise this issue on the Python ideas mailing list, but be prepared to argue a lot over there to gain some traction.
Actually, just not to say “there is no way out”, a third way could be to implement one more level of lambda calling just to unfold the parameters – but that would be at once more inefficient and harder to read than your two suggestions:
min(points, key=lambda p: (lambda x,y: (x*x + y*y))(*p))
Python 3.8 update
Since the release of Python 3.8, PEP 572 — assignment expressions — have been available as a tool.
So, if one uses a trick to execute multiple expressions inside a lambda – I usually do that by creating a tuple and just returning the last component of it, it is possible to do the following:
>>> a = lambda p:(x:=p, y:=p, x ** 2 + y ** 2)[-1] >>> a((3,4)) 25
One should keep in mind that this kind of code will seldom be more readable or practical than having a full function. Still, there are possible uses – if there are various one-liners that would operate on this
point, it could be worth to have a
namedtuple, and use the assignment expression to effectively “cast” the incoming sequence to the namedtuple:
>>> from collections import namedtuple >>> point = namedtuple("point", "x y") >>> b = lambda s: (p:=point(*s), p.x ** 2 + p.y ** 2)[-1]