Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Side-effects in python map (python "do" block)

What is the preferred way to tell someone "I want to apply func to each element in iterable for its side-effects"?

Option 1... clear, but two lines.

for element in iterable:
    func(element)

Option 2... even more lines, but could be clearer.

def walk_for_side_effects(iterable):
    for element in iterable:
        pass

walk_for_side_effects(map(func, iterable))  # Assuming Python3's map.

Option 3... builds up a list, but this how I see everyone doing it.

[func(element) for element in iterable]

I'm liking Option 2; is there a function in the standard library that is already the equivalent?

like image 462
ToBeReplaced Avatar asked Jan 21 '13 21:01

ToBeReplaced


People also ask

Do Python functions have side effects?

We say that the function changeit has a side effect on the list object that is passed to it. Global variables are another way to have side effects. For example, similar to examples you have seen above, we could make double have a side effect on the global variable y.

What does map () do in Python?

Python's map() is a built-in function that allows you to process and transform all the items in an iterable without using an explicit for loop, a technique commonly known as mapping. map() is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable.

Does map mutate list Python?

You don't need recursion if you only work with 2D lists. I know you wrote you don't want to use any for loop , but map is nothing more than a disguised for loop . It also doesn't mutate the list.

Does map work on strings in Python?

Yes. map always returns a map object. No matter the iterable you are napping over. It is meant to work with any iterable.


2 Answers

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).

like image 178
Martijn Pieters Avatar answered Sep 19 '22 22:09

Martijn Pieters


This has been asked many times, e.g., here and here. But it's an interesting question, though. List comprehensions are meant to be used for something else.

Other options include

  1. use map() - basically the same as your sample
  2. use filter() - if your function returns None, you will get an empty list
  3. Just a plain for-loop

while the plain loop is the preferable way to do it. It is semantically correct in this case, all other ways, including list comprehension, abuse concepts for their side-effect.

In Python 3.x, map() and filter() are generators and thus do nothing until you iterate over them. So we'd need, e.g., a list(map(...)), which makes it even worse.

like image 22
Thorsten Kranz Avatar answered Sep 18 '22 22:09

Thorsten Kranz