[Tutor] printing variable name and value?
Borgulya Gabor
borgulya@pons.sote.hu
Wed, 31 May 2000 21:48:21 +0200 (CEST)
Hello,
I have found a working, but unstable solution for my own question:
import traceback
def prvar(__x):
print traceback.extract_stack(limit=2)[0][3][6:][:-1],"=",__x
Lets's test it:
a=5; b=3
prvar(a)
prvar(a+b)
for i in range(10,8,-1):
prvar(i)
The output:
a = 5
a+b = 8
i = 10
i = 9
It works, and I am going to use it for testing my scripts. But in many
cases this method will fail:
prvar (a) # because there is a space between prvar and the '('
(prvar(a)) # such problems could be solved by cleverer search
# for the parameter in the traceback string
prvar(a);prvar(b) # would confuse the cleverer algorithms too
Unfortunately, the outcome is unpredictable when running the script from
IDLE. I don't understand, why.
Yours,
Gabor