Writing to locals(), globals(), and vars()

Michael Haggerty mhagger at alum.mit.edu
Sat Sep 2 19:19:53 EDT 2000


First question: Is there a way to write to the local symbol table in a
generic way (without using `exec')?  For example, this could be used
to return values from a database query:

    db.StoreRowToDict(storeto=locals()) # DOESN'T WORK!

or

    locals().update(db.FetchRowAsDict()) # DOESN'T WORK!

Neither can work because one cannot write to locals().  I quote from
the Python Library Reference:

locals()
    Return a dictionary representing the current local symbol
    table. Warning: the contents of this dictionary should not be
    modified; changes may not affect the values of local variables
    used by the interpreter.

The same caveat is listed for the built-in function vars().

Second question: Is it legal to write to globals()?  The Library
Reference is mute on this point.  (Not that it would help with the
above problem...)

Thanks,
Michael

-- 
Michael Haggerty
mhagger at alum.mit.edu



More information about the Python-list mailing list