Python importing class attributes into method local namespace

前端 未结 3 1177
鱼传尺愫
鱼传尺愫 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条回答
  •  梦毁少年i
    2021-02-14 01:29

    You can easily solve this problem with a tradeoff, by storing the variables in a dictionary.

    data = {}
    copy_to_local_variables = ["a", "b", "c", "d"]
    for var_name in copy_to_local_variables:
        data[var_name] = getattr(self, var_name)
    

    (Though I am unable to understand why you need to copy class attributes to method local namespace)

提交回复
热议问题