Safe to modify globals(), or not?
aahz at pythoncraft.com
Fri Jan 30 21:59:50 CET 2004
In article <bve8f3$us$01$1 at news.t-online.com>,
Peter Otten <__peter__ at web.de> wrote:
>> In article <bvd6hm$9h1$03$1 at news.t-online.com>,
>> Peter Otten <__peter__ at web.de> 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.
>> Yes, but how do you access that from a module?
>setattr(sys.modules["__main__"], "name", "value")
>__main__.anotherName = "another value"
>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
>Or is this a misunderstanding continued?
<smacks head> No, it's a brain fart; for some reason, I was thinking
that "__main__" == "currently executing module".
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
More information about the Python-list