Python: load variables in a dict into namespace

前端 未结 7 735
天命终不由人
天命终不由人 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 13:02

    This is perfectly valid case to import variables in one local space into another local space as long as one is aware of what he/she is doing. I have seen such code many times being used in useful ways. Just need to be careful not to pollute common global space.

    You can do the following:

    adict = { 'x' : 'I am x', 'y' : ' I am y' }
    locals().update(adict)
    blah(x)
    blah(y)
    
    0 讨论(0)
提交回复
热议问题