Abhaya
Abhaya

Reputation: 207

python globals dictionary

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.

Upvotes: 4

Views: 7067

Answers (1)

Ethan Furman
Ethan Furman

Reputation: 69021

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.

Upvotes: 4

Related Questions