Please translate this easy snip of C++ to Python

Clarence Gardner clarence at netlojix.com
Sun Feb 4 22:11:57 CET 2001


On Sun, 04 Feb 2001, chris at onca.catsden.net wrote:
>On 4 Feb 2001, Phlip wrote:
[C debugging macro snipped]

>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

You also need to provide the TRACE function with the caller's local
namespace, or TRACE will, in general, get NameErrors.

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

and call it like
    TRACE('y+z', locals())

(I know I'm stomping on a predefined function name in TRACE, but I
have no problem with it in this case.)
--
Clarence Gardner
Software Engineer
NetLojix Communications
clarence at netlojix.com




More information about the Python-list mailing list