Side-effects in Python map (Python “do” block) [duplicate]

Avoid the temptation to be clever. Use option 1, it’s intent is clear and unambiguous; you are applying the function func() to each and every element in the iterable.

Option 2 just confuses everyone, looking for what walk_for_side_effects is supposed to do (it certainly puzzled me until I realized you needed to iterate over map() in Python 3).

Option 3 should be used when you actually get results from func(), never for the side effects. Smack anyone doing that just for the side-effects. List comprehensions should be used to generate a list, not to do something else. You are instead making it harder to comprehend and maintain your code (and building a list for all the return values is slower to boot).

Leave a Comment