The Voidspace Techie Blog

Your example with sorted can also be written using operator.itemgetter(), I'm pretty sure this will be faster:

new_list = sorted(old_list, key=operator.itemgetter(1))


Gravatar Ok, what about :

new_list = sorted(old_list, key=lambda x: abs(x[1]))

The point is, that I think the lambda here is perfectly readable...


Gravatar I also believe lambda has its place, but I think I'd write all your examples without it.

The first example can be written with itemgetter (though I agree for slightly more complicated versions like your abs(x[1]), I'd use lambda).

If there are lots of examples like the second, I'd probably write a function to abstract the function creation and end up with:

new_data = process(data_Set, make_adder(1))
new_data2 = process(data_Set, make_adder(-1))

(though I'd likely use lambda inside make_adder)

For the third, the property is shorter, but its a lot more cluttered and harder to read to me than:

@property
def value(self):
return self.__value

I do use lambda quite frequently, but it is less readable than the alternatives for a lot of things.


Name:

Email:

URL:

Comment:  ? 

 

Commenting by HaloScan