3 ms·
Well, for one I believe they pre-date comprehensions. Having them as functions is also occasionally useful for partial application, e.g. from functools impor
by clusmore 10y ago
Well, for one I believe they pre-date comprehensions. Having them as functions is also occasionally useful for partial application, e.g.
from functools import partial
to_strings = partial(map, str)
# vs
def to_strings(seq):
return (str(elem) for elem in seq)
- m_mueller 10y agoI can see it together with partial, yes, that's when it can become a bit cleaner. Another reason why I use map is when I want to use multiprocessing or multithreading (with IO heavy functions). But on a fine grained level of code I find it really hurts readability compared to comprehensions.
- EvilTerran 10y agoOr this: def to_strings(seq): return map(str, seq) Generally, when the operation I'm applying to each element happens to already be a named function, I find "map(f, seq)" preferable to "(f(x) for x in seq)".