0
votes

I Have that code:

    class Welcome(object):
        MyHouse='Earth'
        def say(self, what):
            print(what, self.MyHouse)

    welcome = Welcome()

    Living = ['World', 'Planet', welcome.MyHouse]

    MyChoice = list(map(lambda choice : setattr(welcome, 'MyHouse', choice), Living))
    print(''.join(str(MyChoice) for MyChoice in MyChoice))
    getattr(welcome, 'say')('Hello')

with this code , I want to use setattr() to change the MyHouse variable according to the strings contained in the Living list, but when I see the results I only get None objects ?, why this and how can I fix it, it would be because setattr does not give output ?.

    print(MyChoice) 
    Output: [None, None, None] 
1
What did you think setattr would return? - chepner
but the var is not changing - John. C03

1 Answers

0
votes

so, after a coffee I managed to solve, I just needed to use a for loop although I do not know the exact reason why this worked and would like an explanation, I simply thought that the map() would eliminate the need to a for loop. Here is the code:

    class Welcome(object):
        MyHouse='Earth'
        def say(self, what):
            print(what, self.MyHouse)

    welcome = Welcome()

    Living = ['World', 'Planet', welcome.MyHouse]

    MyChoice = map(lambda choice : setattr(welcome, 'MyHouse', choice), Living)
    for i in MyChoice:
        getattr(welcome, 'say')('Hello')

That was a useless code , if i need a for loop why i am using lambda and map.