Please translate this easy snip of C++ to Python

gangli at msn.com gangli at msn.com
Thu Feb 8 16:34:47 CET 2001


In article <BTif6.12144$AH6.1880632 at newsc.telia.net>,
  "Fredrik Lundh" <fredrik at effbot.org> wrote:

How do you make trace("a = 1") work ?


> "Phlip" <phlip_cpp at my-deja.com> wrote:
> Or use something like this:
>
>     def trace(expr):
>         # evaluate expression in callers namespace
>         import sys
>         try:
>             raise None
>         except:
>             frame = sys.exc_info()[2].tb_frame.f_back
>         print expr, "=", eval(expr, frame.f_globals, frame.f_locals)
>
>     x = 10
>     y = 20
>     trace("x + y")
>
>     ## prints:
>     ## x + y = 30
>
> Cheers /F
>
> <!-- (the eff-bot guide to) the standard python library:
> http://www.pythonware.com/people/fredrik/librarybook.htm
> -->
>
>


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list