Global Namespace

Charlie Dyson charlie at charliedyson.net
Sun Jun 17 16:07:20 EDT 2001


Uwe Hoffmann - nospam at nospam.de wrote on Sunday 17 June 2001 18:15:

> 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
> 

Thanks, that's exactly what I wanted to know - the existance of the 
globals() fuction. This should solve some problems for me. As you can 
probably tell, I haven't been using Python for very long.

(Also, I recently discovered I can use nested functions PHP style - is this 
a new feature?)

Thanks again,

Charlie Dyson - charlie at charliedyson.net




More information about the Python-list mailing list