python globals dictionary

烂漫一生 提交于 2019-12-10 15:49:31

问题


I want to merge a dictionary , recieved from another module as a function argument to the globals dictionary of the current module. Any idea how this can be done ?

module - test.py

def setdict(indict):
    somedict = dict(globals(), **indict) 

what i want is, the resultant dictionary somedict is to be set as the globals dictionary of the current module (test) . somedict was created by merging globals() of the current module and the recieved dictionary indict.


回答1:


globals() returns the current module's global dictionary, which you can then modify. Your function would like:

def setdict(indict):
    globals().update(indict)

If there are name clashes, the indict dictionary will win.



来源:https://stackoverflow.com/questions/7463378/python-globals-dictionary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!