Creating or assigning variables from a dictionary in Python

后端 未结 7 1617
一生所求
一生所求 2020-12-06 10:05

I tried to ask a question normally once in here but nobody understands what I want to ask. So I\'ve found example in PHP.

// $_POST = array(\'address\' =>         


        
7条回答
  •  失恋的感觉
    2020-12-06 10:59

    Nothing really new here, just a consolidation of one anwser and an illustration of what @John Y meant in his answer:

    mydict = {'raw': 'data', 'code': 500}
    
    def extract(dct, namespace=None):
        if not namespace: namespace = globals()
        namespace.update(dct)
    
    extract(mydict)
    print raw
    print code
    
    class Extract:
        def __init__(self, dct):
            self.__dict__.update(dct)
    
    obj = Extract(mydict)
    print obj.raw
    print obj.code
    

提交回复
热议问题