Please translate this easy snip of C++ to Python

chris at onca.catsden.net chris at onca.catsden.net
Sun Feb 4 15:07:20 EST 2001


On 4 Feb 2001, Phlip wrote:

> Pythonographers:
>
> Ogle this awesome snip of C++:
>
>         #define TRACE_(x) cout << #x ": " << x << endl
>
> When you use that like this:
>
>         TRACE_(y + z);
>         TRACE_(strAnimals);
>         TRACE_(__LINE__);
>
> the program emits this:
>
>         y + z: 12
>         strAnimals: Lemmings
>         __LINE__:  69
>
> Mighty healthy and easy to write debug statements with, huh? Saves a lot of
> keystrokes, huh?
>
> How do I do that, just as easy to call, in Python?

Try this:

def TRACE ( str ):
	print "%s: %s" % ( str, eval(str) )

TRACE('y+z')
TRACE('strAnimals')

Sorry... you /do/ need to put the parameters in quotes, so Python doesnt
try to evaluate them before calling TRACE

And... not sure if there's a equivalent of __line__


   ("`-/")_.-'"``-._        Ch'marr, a.k.a.
    . . `; -._    )-;-,_`)  Chris Cogdon <chmarr at furry.org.au>
   (v_,)'  _  )`-.\  ``-'
  _.- _..-_/ / ((.'       FC1.3: FFH3cmA+>++C++D++H++M++P++R++T+++WZ++Sm++
((,.-'   ((,/   fL               RLCT acl+++d++e+f+++h++i++++jp-sm++





More information about the Python-list mailing list