Can I use a map to apply a function to an argument list and ignore the results?

Do you find it normal to use a map to apply a function in the argument list and ignore the results?

map(foo, bar)

It may appear as an error for the person who is reading the code.

+3
source share
3 answers

If you want the result using a map to be a great way to apply a function to each element in a list, although many find it clearer to write it down as an understanding or list generator:

result = [foo(x) for x in bar]

However, if you are not going to use the result of the function call and are only interested in side effects, then you should write a procedural style instead:

for x in bar:
    foo(x)
+8

. for, .

+6

:

from itertools import imap, ifilterfalse

tuple(ifilterfalse(None, imap(lambda x: x+1, range(10))))

That tuplewill always be empty (given this input).

0
source

Source: https://habr.com/ru/post/1752660/


All Articles