Safe to modify globals(), or not?

Robert Dodier robert_dodier at yahoo.com
Fri Jan 30 00:59:27 CET 2004


Hello,

I'm interested in introducing new variables into the environment
of a Python interpreter or program. 

In reading through old posts to this newsgroup, I see there is
an often-repeating warning against modifying the contents of locals().
Fair enough. However, is there anything wrong with modifying globals()
?

In the list of built-in functions
(http://www.python.org/doc/lib/built-in-funcs.html)
locals() has an explicit warning, globals() doesn't. Does that mean
it's safe to modify globals() ?

By the way, is there another way to introduce a new variable into
a scope (local or global) other than assigning directly to the 
dictionary returned by locals() or globals() ?

Just some context -- I want to parse strings written in a 
"little language" and then have new variables show up in the Python
environment so the user can manipulate them. E.g.,

  >>> parse_funky_language("Hey, this is far out, man.")
  >>> Hey.part_of_speech
  Interjection

Any light you can shed on this issue will be appreciated.
Thanks for not getting distracted about whether this is useful. 

regards,
Robert Dodier
--
If I have not seen as far as others, it is because 
giants were standing on my shoulders. -- Hal Abelson



More information about the Python-list mailing list