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
声明这个类后,我写了这段代码:
>>> 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']
我无法理解 map 函数是如何在幕后工作的,为什么它会返回一个包含所有内容的列表None
,但是当我检查f.goodFood
元素是否已添加到那里时?