Map method in python

class FoodExpert: def init(self): self.goodFood = [] def addGoodFood(self, food): self.goodFood.append(food) def likes(self, x): return x in self.goodFood def prefers(self, x, y): x_rating = self.goodFood.index(x) y_rating = self.goodFood.index(y) if x_rating > y_rating: return y else: return x 

After declaring this class, I wrote this code:

 >>> f = FoodExpert() >>> f.init() >>> map(f.addGoodFood, ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']) [None, None, None, None, None] >>> f.goodFood ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'] 

I can’t understand how the map function behind the hood works, why it returns a list with all None , but when I check f.goodFood , were there elements added?

+8
python list functional-programming
source share
5 answers

map applies the function to iteration and returns a new list in which the function was applied for each element.

In your case, it shows None , because the function f.addGoodFood does not return anything.

For testing purposes, modify addGoodFood as follows:

 def addGoodFood(self, food): self.goodFood.append(food) return "test" 

and look:

 >>> map(f.addGoodFood, ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']) ['test', 'test', 'test', 'test', 'test'] 
+9
source share

This is because addGoodFood nothing. Do something back:

 def addGoodFood(self, food): self.goodFood.append(food) return food 

map creates a list of addGoodFood for each item in the list. And since the list append method always returns None , you get a None list.

Alternatively, you can change your init function to this:

 def __init__(self): self.goodFood = [] 

__init__ is a special method that handles class initialization. Using this means you do not need to do f.init() .

+5
source share

The code in the question returns a list with None elements, because addGoodFood() does not explicitly return anything (implicitly, it will return None ). map() collects the results of the function you are calling and creates a new list with these results.

You should not use map() with a function that does not return a value. In this case, addGoodFood() not a good candidate for using it. Here is how you should write it:

 for food in ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise']: f.addGoodFood(food) 

To answer the question: how does map() under the hood? here as:

 def map(func, lst): result = [] for e in lst: result.append(func(e)) return result 

In the above function, it is obvious why you get a list of None elements as a result: if the passed function returns None , then the loop will execute result.append(None) , and the returned list will contain only None at each position.

+4
source share

result = map(function, iterable) equivalent:

 result = [] for item in iterable: result.append(function(item)) 

Thus, internally, he creates a list in which he adds the result of applying the function to each element in the list.

Since your addGoodFood function addGoodFood nothing, the map function returns a list of None s.

Your function has a side effect to add it to the goodFoods list, the goodFoods list goodFoods populated and contains all the elements. But since, as I said, the function returns nothing that the map returned a list that is the same size as the original list (since it applies to each element), full None s.

+3
source share

Because you did not return anything from the addGoodFood function.

+3
source share

All Articles