Please translate this easy snip of C++ to Python
fredrik at effbot.org
Sun Feb 4 21:24:01 CET 2001
"Phlip" <phlip_cpp at my-deja.com> wrote:
> Ogle this awesome snip of C++:
> #define TRACE_(x) cout << #x ": " << x << endl
That's a preprocessor trick, not a C++ trick. Replace <<
with printf, and it works just as fine under C.
(iirc, bjarne thinks that real C++ programmers shouldn't
use the preprocessor at all, but what does he know...)
> How do I do that, just as easy to call, in Python?
Run your Python code through C's preprocessor?
Or use something like this:
# evaluate expression in callers namespace
frame = sys.exc_info().tb_frame.f_back
print expr, "=", eval(expr, frame.f_globals, frame.f_locals)
x = 10
y = 20
trace("x + y")
## x + y = 30
<!-- (the eff-bot guide to) the standard python library:
More information about the Python-list