Python importing class attributes into method local namespace

前端 未结 3 1182
鱼传尺愫
鱼传尺愫 2021-02-14 00:57

I have been wondering for a while if there is easier way to assign class attributes to method local namespace. For example, in dosomething method, I explicitly make

3条回答
  •  星月不相逢
    2021-02-14 01:26

    I found another way:

    def dosomething(self):
        for key in ['a', 'b']:
           exec('{} = self.{}'.format(key, key))
    
        return(a + b)
    

    I don't know if this is dangerous or not. Would be great if someone can comment on this.

提交回复
热议问题