Python: load variables in a dict into namespace

前端 未结 7 758
天命终不由人
天命终不由人 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:46

    Rather than create your own object, you can use argparse.Namespace:

    from argparse import Namespace
    ns = Namespace(**mydict)
    

    To do the inverse:

    mydict = vars(ns)
    

提交回复
热议问题