Safe to modify globals(), or not?

Peter Otten __peter__ at web.de
Fri Jan 30 19:40:35 CET 2004


Aahz wrote:

> In article <bvd6hm$9h1$03$1 at news.t-online.com>,
> Peter Otten  <__peter__ at web.de> wrote:
>>Aahz wrote:
>>>
>>> import __main__
>>> tmp = parse_funky_language("Hey, this is far out, man.")
>>> setattr(__main__, tmp.name, tmp.value)
>>> 
>>> In the context of the interactive interpreter, it's a bit harder to do;
>>> I don't remember off-hand what the namespace of the interpreter is.
>>
>>You don't need to :-)
>>
>>Python 2.3.3 (#1, Jan  3 2004, 13:57:08)
>>[GCC 3.2] on linux2
>>Type "help", "copyright", "credits" or "license" for more information.
>>>>> __name__
>>'__main__'
> 
> Yes, but how do you access that from a module?

<interp.py>
import sys
import __main__
setattr(sys.modules["__main__"], "name", "value")
__main__.anotherName = "another value"
</interp.py>

Python 2.3.3 (#1, Jan  3 2004, 13:57:08)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import interp
>>> name
'value'
>>> anotherName
'another value'
>>>

Or is this a misunderstanding continued?

Peter



More information about the Python-list mailing list