Python: load variables in a dict into namespace

前端 未结 7 754
天命终不由人
天命终不由人 2020-11-27 12:15

I want to use a bunch of local variables defined in a function, outside of the function. So I am passing x=locals() in the return value.

How can I load

7条回答
  •  醉话见心
    2020-11-27 12:56

    There's Always this option, I don't know that it is the best method out there, but it sure does work. Assuming type(x) = dict

    for key, val in x.items():  # unpack the keys from the dictionary to individual variables
        exec (key + '=val')
    

提交回复
热议问题