Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (12.7k points)

I can't see how the map() is functioning behind the hood, for what reason is it returning a rundown with all None, except for when I check f.goodFood the components have been added there?

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

I also wrote this code, after declaring this class:

>>> 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']

1 Answer

0 votes
by (26.4k points)

Here, map applies a capacity/function on an iterable and returns another list where the function was applied on every item. 

For your situation, it shows None on the grounds that f.addGoodFood work brings nothing back. 

For testing purposes change addGoodFood along these lines:

def addGoodFood(self, food):

    self.goodFood.append(food)

    return "test"

and see:

>>> map(f.addGoodFood, ['SPAM', 'Eggs', 'Bacon', 'Rat', 'Spring Surprise'])

['test', 'test', 'test', 'test', 'test']

Interested to learn python in detail? Come and Join the python course.

Related questions

0 votes
1 answer
asked Jul 6, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.5k questions

29.9k answers

500 comments

99.1k users

Browse Categories

...