Destructuring-bind dictionary contents

前端 未结 12 1089
無奈伤痛
無奈伤痛 2020-12-02 15:24

I am trying to \'destructure\' a dictionary and associate values with variables names after its keys. Something like

params = {\'a\':1,\'b\':2}
a,b = params.         


        
12条回答
  •  渐次进展
    2020-12-02 15:29

    Well, if you want these in a class you can always do this:

    class AttributeDict(dict):
        def __init__(self, *args, **kwargs):
            super(AttributeDict, self).__init__(*args, **kwargs)
            self.__dict__.update(self)
    
    d = AttributeDict(a=1, b=2)
    

提交回复
热议问题