3 ms·
> Also, once map(), filter() and reduce() are gone, there aren't a whole lot of places where you really need [lambda]. By far the most common use of lambda for
by avian 8y ago
> Also, once map(), filter() and reduce() are gone, there aren't a whole lot of places where you really need [lambda].
By far the most common use of lambda for me is the "key" argument for sort() or sorted(). As in something like:
foo.sort(key=lambda x:x[2])
Yes, there are itemgetter and attrgetter in operator module, but I always have to look up their names and writing the lambda expression is simply faster.
- deleted 8y ago[deleted]
- nszceta 8y agoitemgetter and attrgetter are compiled C functions called by cpython https://github.com/python/cpython/blob/master/Modules/_operator.c https://github.com/python/cpython/blob/master/Modules/_opera...
- masklinn 8y agoThey mean faster to write, not that it's necessarily faster to execute (though the difference is likely immaterial).
- thomasahle 8y agoRight, though for those uses something like the swift {$0[0]} or haskell (!!0) just seems a lot more of convenient.
- raverbashing 8y agoYeah, I don't get the "hate" for the functional operations, though comprehensions have better syntax And no, I don't want to 'def' every small function I might need, pep-8 recommendations aside. Key as mentioned is one use, but there are other cases where it's especially useful