backend/python

python: make variable from dict

seul chan 2018. 7. 25. 21:00

Use exec

d = {'a':1, 'b':2}
for key,val in d.items():
    exec(key + '=val')

# one line
list(map(exec, ("{key}={value}".format(key=x[0], value=x[1]) for x in d.items())))

Using Namespace

from argparse import Namespace

d = {'a':1, 'b':2}
n = Namespace(**d)


'backend > python' 카테고리의 다른 글

python: photo encrypt using python  (0) 2018.11.28
ipython: copy and paste without indentation  (0) 2018.10.17
python: pyenv, virtualenv, auto in mac os  (0) 2018.06.28
python: make flat list  (0) 2018.06.22
python: make uuid in python  (0) 2018.06.04