Consider the following python2 code
In [5]: points = [ (1,2), (2,3)]
In [6]: min(points, key=lambda (x, y): (x*x + y*y))
Out[6]: (1, 2)
This is not supported in python3 and I have to do the following:
>>> min(points, key=lambda p: p[0]*p[0] + p[1]*p[1])
(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 star
import functools
def star(f):
@functools.wraps(f)
def f_inner(args):
return f(*args)
return f_inner
lambda
to get removed from the language entirely then to reverse changes that made it harder to use, but you could try posting on python-ideas if you'd like to express a desire to see the feature added back.lambda
in the same spirit as he opposesmap
,reduce
andfilter
.lambda
was slated for removal in py3k as it's basically a blight on the language. But nobody could agree on a proper alternative for defining anonymous functions, so eventually Guido threw up his arms in defeat and that was that.map
andfilter
are best replaced by comprehensions, I do likereduce
)