In C/C++, I have often found it useful while debugging to define a macro, say ECHO(x)
, that prints out the variable name and its value (i.e. ECHO(variable)
might print variable 7
). You can get the variable name in a macro using the 'stringification' operator #
as described here. Is there a way of doing this in Python?
In other words, I would like a function
def echo(x):
#magic goes here
which, if called as foo=7; echo(foo)
(or foo=7; echo('foo')
, maybe), would print out foo 7
. I realise it is trivial to do this if I pass both the variable and its name to the function, but I use functions like this a lot while debugging, and the repetition always ends up irritating me.