Global Namespace

Uwe Hoffmann nospam at nospam.de
Sun Jun 17 14:15:33 EDT 2001


Charlie Dyson wrote:
> 
> This question either has a simple answer, or there is no way to do this at
> the moment. I want to be able to set a variable in the global namespace to
> a given value. Easy? What if all you have is a string containing the name
> of the variable. I need a pointer to the global namespace. So
> 
> class MyClass:
>         def mymethod(self, varname, varvalue):
>                 setattr(pointer_to_global_namespace, varname, varvalue)

i think you can use:
globals()[varname] = varvalue
instead of setattr(....)
> 
> m = MyClass()
> m.mymethod('var', 'ASDF')
> print var # Should print 'ASDF'
> 
> Any ideas? Is there a __builtins__ funtion that provides a
> reference/pointer/whatever you call it to the global namespace.
> 
> Thanks in advance,
> 
> Charlie Dyson - charlie at charliedyson.net



More information about the Python-list mailing list