Subject: Re: I give up... was: A = X > Y ? X : Y - DN [1]


"Fredrik Lundh" <effbot@telia.com> - 16 Feb 2000 - comp.lang.python

 Michal Wallace <sabren@manifestation.com> wrote:
 > Fredrik Lundh wrote in message ...
 > >the problem here is that you'll evaluate the
 > >expression in the function's own namespace,
 > >not the callers.
 > >
 > >(you can use trickery or guile to get around
 > >that, but I won't post that solution...)
 >
 > Okay, I give up... How do you:
 >
 > a) know what the caller's namespace even IS
 > b) evaluate something in that namespace?

 look carefully:

 import sys

 def magic_eval(s):
     try:
         raise None
     except:
         frame = sys.exc_traceback.tb_frame.f_back
         return eval(s, frame.f_globals, frame.f_locals)

 > Wouldn't that be useful in general for
 > experimenting with language enhancements,
 > even if the code were a little hairy?

 sure.  if you know what you're doing ;-)

 def run_me():
     a = "it "
     b = "works!"
     print magic_eval("a+b")

 </F>

Last modified
2000-03-11

(195.108.246.55)

Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated