Puzzle: anonymous tuple unpacking w/o eval

Edward K. Ream edreamleo at charter.net
Fri May 16 09:48:56 EDT 2003


>   __import__(__name__).__dict__.update(dict(tuple))
>
> (or, perhaps,
>
>   globals().update(dict(tuple))

Spectacular!

Is there a way to do something similar with locals() so as not to affect the
global namespace? The documentation sternly warns against modifying
locals().

Edward

P.S. Your answer also answers another question I've had for a long time.  If
f is a function object, we can use inspect to get any kind of info about f.
But how to get the function object for a function f within f itself?  Now
it's clear:

   globals().get("f")

or anonymously:

   globals().get(sys._getframe(0).f_code.co_name)

Many thanks for unlocking this door.

EKR
--------------------------------------------------------------------
Edward K. Ream   email:  edreamleo at charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------






More information about the Python-list mailing list