我有密码:
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')
在这段代码中,我想使用setattr()根据Living list中包含的字符串来更改MyHouse变量,但是当我看到结果时,我只得到None objects?,为什么要这样做?如何修复它,这是因为setattr不提供输出
print(MyChoice) Output: [None, None, None]
喝了杯咖啡后我终于解决了, 我只需要使用for循环 虽然我不知道为什么这样做的确切原因,并且希望得到解释,但我只是认为map()可以消除for循环的需要。 代码如下:
这是一个无用的代码,如果我需要一个for循环为什么我使用lambda和map
相关问题 更多 >
编程相关推荐