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?
python list functional-programming
Abkds
source share