Insert variable into global namespace from within a function? [duplicate]

It is as simple as

globals()['var'] = "an object"

and/or

def insert_into_namespace(name, value, name_space=globals()):
    name_space[name] = value

insert_into_namespace("var", "an object")

Remark that globals is a built-in keyword, that is, 'globals' in __builtins__.__dict__ evaluates to True.

Leave a Comment